Forums

[resolved] Show latest posts but no double posts (3 posts)

  1. nandayo
    Member
    Posted 3 years ago #

    Hello,

    I’m currently using the query:

    <?php
    	$categories = get_categories('hide_empty=1');
    	foreach ($categories as $category) :
    	query_posts("showposts=1&cat=" .$category->cat_ID);
    	if (have_posts()) : the_post();
    	?>

    to show the latest post from every category on my front page.

    Because I’m using child categories I have many double posts on my front page. How can I remove the double posts? (I have a category named CAT and a child category named CHILD. CAT is empty and CHILD containts a post. Why does WP show up the post two times? :-( )

    Thank you!

  2. MichaelH
    Volunteer
    Posted 3 years ago #

    Assuming you don't put posts in more that one category, this should work:

    <?php
    $categories = get_categories('hide_empty=1');
    foreach ($categories as $category) {
      if ($category->count > 0 ) {
        $args=array(
          'cat' => $category->cat_ID,
          'showposts'=>1,
          'caller_get_posts'=>1
        );
        $catposts=get_posts($args);
        if ($catposts) {
          foreach($catposts as $catpost) {
            echo "<a href='".get_permalink($catpost->ID)."'>".$catpost->post_title."</a>
    ";
          }
        }
      }
    }
    ?>

    Note:
    By WordPress standards, ANY category that has been designated a Parent category is not meant to be checked in the category hierarchy when writing posts. Meaning, only the latest generation category should be checked. Think of it like this, only the youngest generation gets the action.

  3. nandayo
    Member
    Posted 3 years ago #

    Thank you very much, Michael!

Topic Closed

This topic has been closed to new replies.

About this Topic