wordpress combine page and posts - php

I am creating a wp template based on 2013. I want to display a 'page' that has some content from a page and then 6 posts on it. These posts have been selected via the themes options panel using the settings api. So I can get at each one using
$options = get_option( 'osc_theme_options' );
The template i have been using so far is based on a page so i am guessing I need to change the loop in someway.
The loop goes:
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...
I want to know how to alter the loop so that it pulls in just the posts that have been selected. At the moment this template/loop is only pulling in the page - which I guess is fair enough.
Would it be possible to create 'another loop' maybe under the first that then brings in the selected posts - if so how?
Many thanks

Yes you can effectively create another loop under the existing loop to display the posts. I am not sure what $options = get_option( 'osc_theme_options' ); returns, i.e an array of Post ID's etc. In order to show the posts you need to do a custom loop:
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() )
{
echo '<ul>';
while ( $the_query->have_posts() )
{
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
}
else
{
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
This is taken from:
https://css-tricks.com/snippets/wordpress/custom-loop-based-on-custom-fields/
Also see the following:
https://digwp.com/2011/05/loops/
http://www.smashingmagazine.com/2013/01/14/using-wp_query-wordpress/
So effectively it all boils down to the $args variable as to which posts you will get. Here is an example of multiple ids
id=2,6,17,38
So if $options = get_option( 'osc_theme_options' ); returns an array of post ids like so:
array(
0=>1
1=>22
2=>27
)
You could probably do something like:
$args = "id=".implode($options,",");
This is the best advice I can give without deeper knowledge of the theme etc.

Related

Looping through CMB2 attached posts

I've set up two different custom post types – one for categories and one for posts. I've attached the posts to the categories with the CMB2 plugin. I now want to display all the attached/related posts on each category page. I'm able to display the different ID's from the array, but not the post content.
Trying to get attached posts:
$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );
foreach ( $attached_users as $user ) {
$employee = get_post( $user );
}
Trying to display the content from the attached posts
<?php while ( have_posts() ) : the_post(); ?>
<?php echo get_the_title($employee);?>
<?php echo get_the_post_thumbnail($employee);?>
<?php endwhile; // end of the loop. ?>
The plugin you are using gives you an array of ids.
Using this ids gives you the possibility to get the post object by using get_post($id).
The function gives you a post object: https://developer.wordpress.org/reference/functions/get_post/
But if you just want to use functions like get_the_title() you only need the post id, not the whole post object.
Well, you already got all the data you need and do not need to get the post object.
The plugins gives you an array of post ids.
// get array of post ids
$attached_users = get_post_meta( get_the_ID(), 'pr2_cmb2_attached_posts', true );
// loop through posts using post ids
foreach ( $attached_users as $user ) {
echo get_the_title( $user );
echo get_the_post_thumbnail( $user, 'thumbnail' );
}

Find all Wordpress posts that contain a certain word

Without adding any tags or categories, I need a way to generate a page that lists all Wordpress posts containing the word, for example, "design" somewhere within them. Does anyone know how to do this?
You can use WP_Query to do a search:
<?php
$query = new WP_Query( 's=keyword' );
if ($query->have_posts()){
while ( $query->have_posts() ) { $query->the_post();
echo '<h2>';
the_title();
echo '</h2>';
the_content();
} //end while
}
Let me know how it went!

Get Page by TAG ID in Wordpress

so i have a php script that need to retrieve a certain "page", not a post but a page, in wordpress, i want to retreive ONE page that is pointed in a specific tag. I would like to know how i can retreive that certain page, the only parameter it needs is the tag->term_id.
I tried searching in google to acheive this. is this possible?
Thanks..
use WP_Query with post_type set to page to query your database for pages. You can use the tag_id parameter to narrow down by tag. Limit it to one result using posts_per_page set to 1.
//narrow down your query with $args
$args = array('post_type'=>'page', 'tag_id'=>3, 'posts_per_page'=>1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
http://codex.wordpress.org/Class_Reference/WP_Query

Get content of specific page in WordPress

at the moment I am querying out all pages that I have in WordPress and am displaying them on a page. Like this:
<?php
query_posts(array('post_type' => 'page'));
while(have_posts())
{
$this_page = the_post();
echo the_content();
}
?>
The question is, how can I do the same thing but just for one specific post? As a side note, if I need to use id's of pages where can I find them, as at the moment I don't see what id page has in WordPress.
take a look at the page on query_posts in the wordpress codex here: http://codex.wordpress.org/Function_Reference/query_posts .
Adjust arguments accordingly. For example, to query a post with a specific id:
query_posts( 'p=5' );
something like this?
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );//al items from my-category-slug
while(have_posts())
{
$this_page = the_post();
echo the_content();
}

How can I have wordpress print posts whose IDs appear in an array?

I have an array of post IDs contained in $postarray. I would like to print the posts corresponding to these IDs in Wordpress.
The code I am using is as follows:
query_posts(array('post__in' => $postarray));
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
the_excerpt();
endwhile;
endif;
Despite this, the loop prints the most recent posts and not the posts contained in the array. How can I have wordpress utilize the post IDs I supply in the array and print those posts in order?
You may have to break out of the standard WP Loop for this...
Try and use the get_post() function which takes the ID of a post and returns an object containing a the details of the post in the usual OBJECT or Associate or Numeric Array format.
See full-explanation of get_post().
You can come up with a custom routine to parse each item in the array. Here's a brief example:
function get_posts_by_ids( $postarray = null ) {
if( is_array( $postarray ) )
foreach( $postarray as $post ) {
$post_details = get_post( $post[0] );
// Title
echo $post_details->post_title;
//Body
echo $post_details->post_content ;
}
}
Hope this helps :)

Categories