1

I am building a category overview page and i am use this code:

<?php
    $limit      = 999;
    $counter    = 0;
    $categories = get_categories();

    foreach ($categories as $category):
        if ($counter < $limit) {
            $args  = array(
                'category__in' => array(
                    $category->term_id
                ),
                'caller_get_posts' => 1
            );
            $posts = get_posts($args);
            if ($posts) {
                echo '<div class="category">';
                echo '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>';
                echo '<h3>' . $category->name . '</h3>';
                echo '</a>';
                echo '</div>';

            }
        }

    $counter++;
    endforeach;
?>

But how to insert in the echo that the featured image from the last post in that category will be displayed.

  • Do you want to show featured image of the latest post of each category?? – Bikash Waiba May 3 '18 at 11:01
1

This should work. I've modified your $args a little and surrounded your echoed code within the while(have_posts()) : the_post(); loop, then added the the_post_thumbnail() function and, finally, the wp_query_reset(); at the bottom:

<?php
    $limit      = 999;
    $counter    = 0;
    $categories = get_categories();

    foreach ( $categories as $category ):
        if ( $counter < $limit ) {
            $args  = array(
                'posts_per_page' => 1,
                'cat' => $category->cat_ID,
                'ignore_sticky_posts' => 1
            );
            $posts = get_posts( $args );

            while( have_posts() ) : the_post();
                echo '<div class="category">';
                the_post_thumbnail();
                echo '<a href="' . get_category_link($category->term_id) . '" title="' . sprintf(__("View all posts in %s"), $category->name) . '" ' . '>';
                echo '<h3>' . $category->name . '</h3>';
                echo '</a>';
                echo '</div>';
            endwhile;

        }

    $counter++;
    endforeach;

    wp_query_reset();

?>

Ps. caller_get_posts is deprecated since version 3.1! Use ignore_sticky_posts instead in your $args

  • Hi Mat, this is working but now i get the image and the title of the category in the div and after that it will make a other div category with only the title inside it. (this for every category its somehows duplicate but only with the title?) – Maanstraat May 3 '18 at 12:58
  • @Maanstraat - Can you provide screenshot so I can see what you mean? – Mat May 3 '18 at 13:44
  • i fxed it already (see the answer under you) – Maanstraat May 3 '18 at 14:02
1

I have used Mat his code and fixed some duplicated issue.

The code that works like a charm:

<?php
    $limit      = 999;
    $counter    = 0;
    $categories = get_categories();

    foreach ($categories as $category):
        if ($counter < $limit) {
            $args  = array(
                'posts_per_page' => 1,
                'cat' => $category->cat_ID,
                'ignore_sticky_posts' => 1
            );
            $posts = get_posts($args);
            if ($posts) {
                    echo '<div class="category">';
                    while( have_posts() ) : the_post();
                        the_post_thumbnail();
                    endwhile;
                    echo '<a href="' . get_category_link($category->term_id) . '" ' . '>';
                    echo '<h3>' . $category->name . '</h3>';
                    echo '</a>';
                    echo '</div>';
            }
        }

    $counter++;
    endforeach;
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.