1

I am having this issue with my loops. I am making a shortcode that will be called from the main query. It will display on the front page from the_loop. for some reason that I can't figure out. The second query displays only one post when it should display 3. So the shortcode will go in the page content. In the settings section, I have the front page set as static to "Home" and the blog page set to "Blog" but home is not a template. It is being generated by the index.php page using WordPress 2014 theme. So in the "Home" page content area, I have the shortcode that generates the second loop to get 3 posts from a category called "featured".

 $featured = new WP_Query( array( 'category_name' => $category, 'posts_per_page' => 3 ) );
   if( $featured->have_posts()) : while( $featured->have_posts()): $featured->the_post();
 global $more;
 $more = 0;

 the_title();

 the_content( '<a class="clear" href="'.get_permalink().'"><img class="button" src="'. get_template_directory_uri() .'/images/read-more.png" alt="read more" height="32" width="85" /></a>' );

endwhile; endif;
wp_reset_postdata();

This loop will display the first of the three posts but not the other two.

I also tried using get_posts but that didn't work out any better

  • The code works for me with known good values. – s_ha_dum Dec 18 '13 at 3:11
  • The one post that returns, can you check if it's a post, not a page ? if page, try adding 'post_type' => 'post'. If it's a post, then try adding 'suppress_filters' => true. If none of it works, then there's other solutions too. – Shazzad Dec 18 '13 at 6:48
  • This did not work. I thought it was because the return statement was not in the loop. When I added inside the loop, it displayed the third post instead of the first. Not sure what is going on. – Jamie Dec 19 '13 at 2:37
1

You can also try with following code:

$featured = new WP_Query( array( 'category_name' => $category, 'posts_per_page' => 3 ) );

// The Loop
if ( $featured->have_posts() ) {
    echo '<ul>';
    while ( $featured->have_posts() ) {
        $featured->the_post();
        echo '<li>';
        echo '<div id="title">' . get_the_title() .'</div>';
        echo '<div id="content">' . get_the_content() .'</div>';
        echo '</li>';
    }
    echo '</ul>';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.