V
V
Valentin Popov2019-12-10 08:29:59
css
Valentin Popov, 2019-12-10 08:29:59

How to display subcategory images in Wordpress?

I am using the taxonomy images plugin. I'm trying to display subcategories (with image and text) of certain parent categories on the main page of the site, but for some reason the image cannot be displayed, only the name. Is it even possible to display subcategory images with this plugin?
Here's what I currently have:

<?php 
$cat_id = get_query_var('cat');
$catlist = get_categories('hide_empty=0&child_of=' . $cat_id);
echo "<ul>";

foreach($catlist as $categories)
{
echo '<h1><a href="' . get_category_link( $categories->term_id ) . '" title="' . sprintf( __( "View all products in %s" ), $categories->name ) . '" ' . '>' . $categories->name.'</a> </h1> ';

echo '<div class="categoryoverview clearfix">';
    $terms = apply_filters( 'taxonomy-images-get-terms', '' );
    if ( ! empty( $terms ) ) {

      foreach( (array) $terms as $term ) {
        if($term->term_id == $categories->term_id) {
           print '<a href="' . esc_url( get_term_link( $term, $term->taxonomy ) ) . '">' . wp_get_attachment_image( $term->image_id, '' );
           echo '</a>';
        }
      }
    echo '<p>'. $categories->description; echo '</p>';
}
echo '</div>';
}
echo "</ul>";
?>

This code displays images of parent categories, but child categories only display captions, but no images,

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
EvilNeighbor, 2019-01-12
@EvilNeighbor

Use DevTools and see where exactly the indents are set. It will be more clear

P
profesor08, 2019-01-12
@profesor08

You either screwed up with the height and width, or you don’t finish something. Please provide a working example on jsfidle or codepen.

L
lukoie, 2019-01-12
@lukoie

Your width is set to 62 pixels. Change it, and get smaller indents between the stars.

N
Nevea, 2019-12-10
@elarkov

the condition does not work for you
. on the main page get_query_var('cat'); will be ""
from the documentation parameter
child_of - Get the child categories (including all nesting levels) of the specified category. The parameter specifies the ID of the parent category (the category whose nested categories you want to show).
it follows that there will be no subcategories in $catlist

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question