Comment on Wordpress Page and Posts

I am creating a wp template based on 2013. I want to display a "page" that has content from the page and then 6 posts. These messages were selected using the theme options panel using the api settings. So I can take advantage of each one using the $options = get_option( 'osc_theme_options' );

Template I have used so far is page-based, so I guess I need to change the loop in some way.

Cycle:

<?php /* The loop */ ?>
            <?php while ( have_posts() ) : the_post(); ?>

                <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>...

      

I want to know how to change the loop so that it only pulls in the messages that have been selected. For now, this pattern / loop only pulls into the page, which I think is fair enough.

Is it possible to create "another loop", perhaps below the first one, which then injects the selected messages - if so, how?

Many thanks

+3


source to share


1 answer


Yes, you can effectively create another loop in an existing loop to display messages. I'm not sure what it returns $options = get_option( 'osc_theme_options' );

, i.e. Array of message id, etc. To show the messages you want, do an arbitrary 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 it all boils down to a variable $args

as to what messages you get. Below is an example of severalids

id=2,6,17,38

      

So if it $options = get_option( 'osc_theme_options' );

returns an array of post IDs like:

array(
       0=>1
       1=>22
       2=>27
)

      

Perhaps you could do something like:

$args = "id=".implode($options,",");

      

This is the best advice I can give without deeper knowledge of the topic, etc.

+1


source







All Articles