I have custom post types and categories created with the Types plugin.

I have category images allowed/created with the Categories Images plugin.

I have a loop that displays the posts of a category in <li>s.

Each <li> has a background style property that needs to get the image assigned to that category.

This is my applicable code:

$film_categories = get_terms( 'category' );

    foreach ( $film_categories as $film_category ) {
        $film_category_query = new WP_Query( array(
            'post_type' => 'film',
            'tax_query' => array(
                    'taxonomy' => 'category',
                    'field' => 'slug',
                    'terms' => array( $film_category->slug ),
                    'operator' => 'IN'
        $term_id = get_terms('category');

        if ( $film_category_query->have_posts() ) : while ( $film_category_query->have_posts() ) : $film_category_query->the_post();

        //$film_background_image = apply_filters( 'taxonomy-images-queried-term-image-url', '' );
        //$film_background_color = types_render_field("film-background-color");
        $hide_all_info         = types_render_field("hide-all-info");
        $film_image            = types_render_field("film-image");
        $film_meta_info        = types_render_field("film-meta");

        <?php var_dump($term_id); ?>
            <li id="<?php echo $film_category->{'slug'}; ?>" style="background: url(<?php echo z_taxonomy_image_url($term_id); ?>); background-size: cover;">

When I var_dump($term_id) I get this:

    [0]=> object(stdClass)#380 (9)
        { ["term_id"]=> string(1) "3" ["name"]=> string(10) "Commercial" ["slug"]=> string(10) "commercial" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(1) "3" ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "1" }
    [3]=> object(stdClass)#377 (9)
        { ["term_id"]=> string(1) "5" ["name"]=> string(4) "Reel" ["slug"]=> string(4) "reel" ["term_group"]=> string(1) "0" ["term_taxonomy_id"]=> string(1) "5" ["taxonomy"]=> string(8) "category" ["description"]=> string(0) "" ["parent"]=> string(1) "0" ["count"]=> string(1) "1" }

How do I get the id for each category so that the z_taxonomy_image_url function can use it to retrieve the category image url?

  • 1
    get_terms gets all terms from a taxonomy, I think you want get_the_terms to fetch the terms assigned to each post? – Milo Nov 6 '15 at 21:58
  • Do you mean for $film_categories or $term_id? Or both? – dmoz Nov 7 '15 at 18:39
  • I assume your intention is to 1) get all terms in the taxonomy, and 2) for each term, load all of the posts that are assigned that term, and 3) for each post list the terms that are assigned to it in order to display the images. It's honestly a bit confusing, I think maybe your code is not doing what you intend. – Milo Nov 7 '15 at 18:58

$term_id = get_terms('category'); is unnecessary, the current category object is held within $film_category. $film_category->term_id will give you the ID of the category you are currently fetching posts from.

|improve this answer|||||
  • That does get me the ID number, and thus technically answers my question. The function I'm using, z_taxonomy_image_url still isn't working though (the images/urls are not getting echoed into the background-image source). The plugin's proposed loop code is this: <ul> <?php foreach (get_categories() as $cat) : ?> <li> <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>" /> <a href="<?php echo get_category_link($cat->term_id); ?>"><?php echo $cat->cat_name; ?></a> </li> <?php endforeach; ?> </ul> Any idea what could be the problem with my code compared to that example? – dmoz Nov 9 '15 at 13:02
  • Nevermind! I was still pulling the regular categories from my earlier tests, not the custom taxonomy to which the images were applied. Changed that and now everything's working great! Thanks for your help! – dmoz Nov 9 '15 at 13:08

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.