WordPress.org

Ready to get started?Download WordPress

Forums

Last post from each category works - but if a post belons to 2 cat? (7 posts)

  1. jumust
    Member
    Posted 2 years ago #

    Hi,
    I use the code below to display in my blog page only the last post from each category and IT WORKS PERFECTLY

    <?php // cycle through categories, print 1 post for each category
    $categories=get_categories('orderby=&order=ASC&');
      foreach($categories as $category) {
          $posts=get_posts('showposts=1&cat='. $category->term_id);
    
            foreach($posts as $post) {
              setup_postdata($post); ?>

    So I have i.e :
    Category A --> Last post (a)
    Category B --> Last post (b)
    Category C --> Last post (c)
    .....

    If I publish a post that belongs to Category A and B, in the home page I'll get
    Category A --> Last post (a)
    Category A --> Last post (a) - same post above
    Category C --> Last post (c)

    Is there a way to keep the category B to show up with the last post as before, and only the Category A to show up with the post just published?

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    <?php // cycle through categories, print 1 post for each category
    $categories=get_categories('orderby=&order=ASC&'); $duplicates = array();
      foreach($categories as $category) {
          $posts=get_posts(array('showposts' => 1, 'post__not_in' => $duplicates, 'category__in' => array($category->term_id)));
    
            foreach($posts as $post) {
              setup_postdata($post); $duplicates[] = $post->ID; ?>

    http://codex.wordpress.org/The_Loop#Multiple_Loops_in_Action

  3. jumust
    Member
    Posted 2 years ago #

    Thanks for your answer
    The code you provided didn't do the trick, I'll explain what happened, now I have:

    Category A --> Last post (a)
    Category A --> Second last post (a)

    Here it's the php file I'm using, it may help my explanation http://pastebin.com/DKJQh3ts

    I think something is wrong with category titles too.

    Thanks

  4. expat
    Member
    Posted 2 years ago #

    very complex; u need to flag it on first display, and then query the next value in the second category.....hope someone can show how it can be down easily

  5. jumust
    Member
    Posted 2 years ago #

    Not sure how to figure out what you said...hope someone can help

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    you could try different parameters:

    $posts=get_posts(array('numberposts' => 1, 'post__not_in' => $duplicates, 'category__in' => array($category->term_id)));

    or

    $posts=get_posts(array('posts_per_page' => 1, 'post__not_in' => $duplicates, 'cat' => $category->term_id));

    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
    http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters
    http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters

  7. jumust
    Member
    Posted 2 years ago #

    I tried to play a bit with the code, and added a tag="featured" to show up only post with featured tag in the blog page.

    I get post not duplicated and it's great alchymyth but I get Category title and category link duplicated, so maybe it's because of the code right below the previous code. See here highlighted please

    Can I use something to not duplicate category too?

Topic Closed

This topic has been closed to new replies.

About this Topic