0

I wanted to see if it was possible to display the "newest" post from all top level post categories on the home page using args in the loop? See http://blog.reneerouleau.com/dev/ for example of what I currently have set up. I'm running shoestrap wp.

<?php 
wp_reset_query();

$cats = get_categories('');
foreach ($cats as $cat) :

if($cat->category_parent)  continue; //this line avoids to show posts of sub categories

$args = array(
'posts_per_page' => 1,
'category_name' => $cat->slug,);

query_posts($args); // reset to original
?>

UPDATE Still not working with below updated code - I think I might need to put that while have post loop in the above new code -- any idea?

<?php get_template_part( 'templates/page', 'header' ); ?>
<div id="blogContainer">
<?php do_action( 'shoestrap_index_begin' ); ?> 
<?php if ( !have_posts() ) : ?>
  <div class="alert">
    <?php _e( 'Sorry, no results were found.', 'shoestrap' ); ?>
  </div>
  <?php get_search_form(); ?>
<?php endif; ?>
<?php 
$cats = get_categories('parent=0');
foreach ($cats as $cat) :
  $args = array(
    'posts_per_page' => 1,
    'category__in' => array( $cat->term_id ) );
  $top_cat_query = new WP_Query($args);
  if( $top_cat_query->have_posts() ) while( $top_cat_query->have_posts() ) : 
     $top_cat_query->the_post();
    //output
  endwhile; wp_reset_postdata();
  //..etc..
endforeach; 
?>
<?php while ( have_posts() ) : the_post(); ?>

    <?php do_action( 'shoestrap_in_loop_start_action' ); ?>
    <?php get_template_part( 'templates/content', get_post_format() ); ?>

<?php endwhile; ?>
</div>
<div id="temp"> 
</div>
<div>
<?php if ( $wp_query -> max_num_pages > 1 ) : ?>
  <nav class="post-nav">
    <ul class="pager">
      <li class="previous"><?php next_posts_link( __( '&larr; Older posts', 'shoestrap' ) ); ?></li>
      <li class="next"><?php previous_posts_link( __( 'Newer posts &rarr;', 'shoestrap' ) ); ?></li>
    </ul>
  </nav>
<?php endif; ?>
</div>
<?php do_action( 'shoestrap_index_end' ); ?>'
4
  • Are they not the newest posts by default? Commented Nov 6, 2013 at 4:36
  • I want it to show ONE (the newest) post per each top level category (see the sidebar) on the home page. Currently it's pulling all categories (sub cats included) and not necessarily the newest on top. Commented Nov 6, 2013 at 4:42
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.
    – gmazzap
    Commented Nov 6, 2013 at 5:20
  • "Still not working with below updated code" - you've got a lot going on there, that can potentially interfere; and "not working" is non-descriptive. Can you isolate the recommend code, and verify whether or not it outputs the latest post from each category? Commented Nov 6, 2013 at 14:22

1 Answer 1

1

for example:

<?php 
$cats = get_categories('parent=0');
foreach ($cats as $cat) :
  $args = array(
    'posts_per_page' => 1,
    'category__in' => array( $cat->term_id ) );
  $top_cat_query = new WP_Query($args);
  if( $top_cat_query->have_posts() ) while( $top_cat_query->have_posts() ) : 
    $top_cat_query->the_post();
    //output
  endwhile; wp_reset_postdata();
  //..etc..
endforeach;
?>

(not tested)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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