admin管理员组文章数量:1327483
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
1 Answer
Reset to default 1In 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.
本文标签: categoriesCategory images are not displaying in WordPress
版权声明:本文标题:categories - Category images are not displaying in WordPress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742207416a2433077.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论