WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Showing last post of each category (12 posts)

  1. soulflow132
    Member
    Posted 1 year ago #

    Hi i want to show the last post of each of my categories.

    How can i do it using the query_posts function please.

    Thank you

  2. esmi
    Forum Moderator
    Posted 1 year ago #

    By "last" do you mean the most recent or the oldest?

  3. soulflow132
    Member
    Posted 1 year ago #

    Thank you for your answer .
    By last i mean the most recent

  4. esmi
    Forum Moderator
    Posted 1 year ago #

    Are you customising the main query/Loop? Or creating a secondary Loop? If it's the latter, you should be using
    http://codex.wordpress.org/Function_Reference/get_posts or
    http://codex.wordpress.org/Function_Reference/WP_Query

    But the same parameters apply if you are customising the main query using query_posts().

  5. soulflow132
    Member
    Posted 1 year ago #

    am cutimising the main query loop

  6. esmi
    Forum Moderator
    Posted 1 year ago #

    Try <?php query_posts( $query_string . '&posts_per_page=1' );?>

  7. soulflow132
    Member
    Posted 1 year ago #

    and for the $query_string what should i do in it ?

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    Nothing. Just try the code I posted above.

  9. soulflow132
    Member
    Posted 1 year ago #

    It posts only 1 post last post but not last post of each category :s

  10. esmi
    Forum Moderator
    Posted 1 year ago #

    You have to cycle through all of the categories on your site and then use WP_Query - not query_posts().

    <?php $all_my_cats = get_all_category_ids();
     foreach( $all_my_cats as $my_cat_id ) {
     	$args = array(
    		'cat' => $my_cat_id,
    		'ignore_sticky_posts' => 1,
    		'posts_per_page' => 1,
    		'paged' => $paged
    	);
    	$the_query = new WP_Query( $args );
    	while ( $the_query->have_posts() ) : $the_query->the_post();
    	<h2 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
    	the_content();
    	endwhile;
    	wp_reset_postdata();
     }
     ?>
  11. soulflow132
    Member
    Posted 1 year ago #

    Thank you so much you just saved my life.
    Thanks alot

  12. esmi
    Forum Moderator
    Posted 1 year ago #

    Glad I could help :-)

Topic Closed

This topic has been closed to new replies.

About this Topic