WordPress.org

Ready to get started?Download WordPress

Forums

Duplicate listing of post under category (3 posts)

  1. calebo
    Member
    Posted 1 year ago #

    I wrote a bit of code that prints all post under a category and displaying the feature image of that post.

    However I'm not sure what's wrong with my loop as it's printing the same post X number of times. X being the number of categories I have.

    Here is my code...

    <?php
    	$cats = get_categories();
    	$thumbnails = get_posts();
    	foreach ($cats as $cat) {
    	    $cat_id = $cat->term_id;
    	    if ( has_post_thumbnail($thumbnail->ID, $category->term_id)) {
    		      echo '<li><a href="' . get_permalink( $thumbnail->ID ) . '" title="' . esc_attr( $thumbnail->post_title ) . '">';
    		      echo get_the_post_thumbnail($thumbnail->ID, 'full');
    		      echo '</a></li>';
    	    }
    	}
    ?>
  2. gholem
    Member
    Posted 1 year ago #

    Many had similar problems and the solution was usually to follow the Codex instructions properly.

    See this, this, this, this and this plugin.

  3. calebo
    Member
    Posted 1 year ago #

    Unfortunately none of those do what I want, they hard code the category ID into the code, I want to make it dynamic in the category.php template so that when you visit http://mysite.com/category/cake
    It would list all post under the cake category on the page.

Topic Closed

This topic has been closed to new replies.

About this Topic