1

I want to create on my blog a category loop with the featured image. But i don't know how i do that technical.

You should only see a list of categories and click on them to get the category site. It's like a article loop only with categories.

Thank you for any help :)

enter image description here

closed as too broad by rudtek, Jacob Peattie, cjbj, bueltge, fuxia Jan 24 at 9:22

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

1

Well, in WP the word "loop" is mainly related to posts. It's easier to say that you want to display categories.

All you have to do is to get them using get_categories function and display them.

<?php
    $categories = get_categories( array(
        'orderby' => 'name',
        'order'   => 'ASC'
    ) );
    if ( ! empty( $categories ) ) :
?>
<ul>
    <?php foreach ( $categories as $category ) : ?>
    <li><a href="<?php echo esc_attr(get_category_link( $category->term_id ) ); ?>"><?php echo esc_html( $category->name ); ?></a></li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

And if you want to change the category title to some ACF custom field, then change this part:

<?php echo esc_html( $category->name ); ?>

to:

<?php echo wp_get_attachment_image( get_field('<FIELD_NAME>', $category), 'full' ); ?>

PS. Remember to change to real name of the field. It should return the ID of an image (and not the array - you should set it in field setting). You can also change the 'full' size to some other size.

  • hmm... put the wp_get_attachement_image in the code, but it still doesn't work :( – Mr Sag ich nicht Jan 15 at 6:11
  • <?php echo wp_get_attachment_image( get_field('cat_thumbnail_image', $category), 'thumbnail' ); ?> – Mr Sag ich nicht Jan 15 at 6:11
0

The key to this is get_categories() function.

You can use it as follows, note that I assumed the featured image url is stored in a meta of the category which may not be your case:

$categories = get_categories();
if(count($categories)) {
    foreach($categories as $cat) {
        $catID = $cat->term_id;
        $img = get_term_meta($catID, 'featured_image', true);
        $url = esc_url( get_category_link( $catID ) );
        echo "<a href='$url'><img src='$img' /></a>";
    }
} else {
    echo "No categories!";
}

In case you're using ACF image field to add the featured image to the category, it's better to use get_field('featured_image', 'category_'.$catID) instead of get_term_meta(). The code in that case depends on your field settings, so I encourage you to read the documentation here.

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