Skip to main content
added 1673 characters in body
Source Link

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' ); ?>'

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
?>

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' ); ?>'
added 342 characters in body
Source Link

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
?>

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.

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
?>
Source Link

Custom Blog Loop on Home Page

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.