最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

categories - Category images are not displaying in WordPress

programmeradmin8浏览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) ".png"
  [19]=>
  string(63) ".jpg"
  [21]=>
  string(88) ".png"
  [22]=>
  string(68) ".jpg"

}

var_dump($category) Output

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/wp-content/uploads/2020/08/reghwij.png"
  [19]=>
  string(63) "http://test/wp-content/uploads/2020/08/hgeioiq.jpg"
  [21]=>
  string(88) "http://test/wp-content/uploads/2020/07/dan-freeman-m4-wkz4GV04-unsplash.png"
  [22]=>
  string(68) "http://test/wp-content/uploads/2020/07/green-breaks.jpg"

}

var_dump($category) Output

Share Improve this question edited Aug 6, 2020 at 13:29 Naren Verma asked Aug 6, 2020 at 11:40 Naren VermaNaren Verma 2491 gold badge6 silver badges19 bronze badges 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 – Jacob Peattie 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/questions/352455/… – Naren Verma 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 – Naren Verma Commented Aug 6, 2020 at 11:57
  • @JacobPeattie, I added the var_dump($category) output (only one) in the question – Naren Verma Commented Aug 6, 2020 at 12:04
  • I tried the above code and I am getting every time first image. – Naren Verma Commented Aug 6, 2020 at 12:25
 |  Show 1 more comment

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论