WordPress.org

Support

Support » Themes and Templates » [Resolved] Showing last post of each category

[Resolved] Showing last post of each category

  • soulflow132
    Member

    @soulflow132

    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

Viewing 11 replies - 1 through 11 (of 11 total)
  • esmi
    Forum Moderator

    @esmi

    By “last” do you mean the most recent or the oldest?

    soulflow132
    Member

    @soulflow132

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

    esmi
    Forum Moderator

    @esmi

    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().

    soulflow132
    Member

    @soulflow132

    am cutimising the main query loop

    esmi
    Forum Moderator

    @esmi

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

    soulflow132
    Member

    @soulflow132

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

    esmi
    Forum Moderator

    @esmi

    Nothing. Just try the code I posted above.

    soulflow132
    Member

    @soulflow132

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

    esmi
    Forum Moderator

    @esmi

    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();
     }
     ?>
    soulflow132
    Member

    @soulflow132

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

    esmi
    Forum Moderator

    @esmi

    Glad I could help 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘[Resolved] Showing last post of each category’ is closed to new replies.