0

I have categories list with there respective images. I am able to get the category name and description but I am not getting the category image. I tried the below code.

    <ul>
      <?php
        $categories = get_categories( array(
            'taxonomy'   => 'category',
            'orderby'    => 'name',
            'parent'     => 0,
            'hide_empty' => 0, 
        ) );
        
            foreach ( $categories as $category ) 
            {
            $cat_ID        = $category->term_id;
            $category_name = $category->name;
            $category_desc = $category->description;
            //$category_img = $category->category_images;
            //$category_images = get_option('category_images');?>

           <li>
           <?php
           echo $category_name;
           echo $category_desc;
           echo $category_images; //display the path of image for temporary

              $category_images = get_option('category_images');
        
?>       
   </li>
          <?php } ?>
        </ul>

I am getting the var_dump($category_images) output

array(9) {
  [17]=>
  string(0) ""
  [18]=>
  string(63) "http://test.com/wp-content/uploads/2020/08/reghwij.png"
  [19]=>
  string(63) "http://test.com/wp-content/uploads/2020/08/hgeioiq.jpg"
  [21]=>
  string(88) "http://test.com/wp-content/uploads/2020/07/dan-freeman-m4-wkz4GV04-unsplash.png"
  [22]=>
  string(68) "http://test.com/wp-content/uploads/2020/07/green-breaks.jpg"

}

var_dump($category) Output

enter image description here

6
  • WordPress doesn’t support images for categories out of the box. Are you using a plug-in? Or is this a feature of your theme Commented Aug 6, 2020 at 11:49
  • @JacobPeattie, No I am not using any plugin. I added custom code in the function to add the image. you can refer this link wordpress.stackexchange.com/questions/352455/… Commented Aug 6, 2020 at 11:54
  • @JacobPeattie, After adding the code in the function I got an image upload option in the category page and I am able to add it and edit it. Now I have to show all the images on my category list page prnt.sc/tv26ee and this prnt.sc/tv282i Commented Aug 6, 2020 at 11:57
  • @JacobPeattie, I added the var_dump($category) output (only one) in the question Commented Aug 6, 2020 at 12:04
  • I tried the above code and I am getting every time first image. Commented Aug 6, 2020 at 12:25

1 Answer 1

1

In your code, you store the images in the wp_options table with the key category_images, but there is nothing that tells WordPress that it must fetch the image when loading the taxonomy term (your category).

What you can do is either use get_option('category_images') whenever you need to fetch the image, or add a hook filter like that.

add_filter( 'get_term', function ( WP_Term $_term, string $taxonomy ): WP_Term
{
    if ( 'my_taxonomy_name' === $taxonomy ) {
        $images = get_option( 'category_images' );
        if ( $image = ( $images[ $_term->term_id ] ?? null ) ) {
            $_term->background_image = $image;
        }
    }
    
    return $_term;
}, 10, 2 );

You will then have the background_image property added when you fetch your taxonomy term.

9
  • Sure I will try your answer, I updated my code also, Can you check it. I am getting the first image every time. Commented Aug 6, 2020 at 12:25
  • I have to add this code in function.php? Commented Aug 6, 2020 at 12:29
  • I'll have to double check that. You can store only one image for every term. What is the first image that you always get?
    – Tim
    Commented Aug 6, 2020 at 17:38
  • I was getting the first one. After adding this $category_images[$cat_ID]; my issue got solved. Commented Aug 6, 2020 at 18:07
  • Didn't you use this : $image = $images[ $_term->term_id ]. As in my answer?
    – Tim
    Commented Aug 7, 2020 at 4:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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