maxelcat
maxelcat

Reputation: 1333

wordpress combine page and posts

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

Upvotes: 3

Views: 332

Answers (1)

The Humble Rat
The Humble Rat

Reputation: 4696

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.

Upvotes: 1

Related Questions