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

categories - show thumbnail for subcategory wordpress

programmeradmin4浏览0评论
$args = array('parent' => 17);
$categories = get_categories( $args );
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  
}

from the above code i got the subcategories also i want to display images for subcategories with hyperlink and the above code does not show empty category. help me in this.

$args = array('parent' => 17);
$categories = get_categories( $args );
foreach($categories as $category) { 
    echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    echo '<p> Description:'. $category->description . '</p>';
    echo '<p> Post Count: '. $category->count . '</p>';  
}

from the above code i got the subcategories also i want to display images for subcategories with hyperlink and the above code does not show empty category. help me in this.

Share Improve this question edited Jul 26, 2020 at 11:46 mozboz 2,6281 gold badge12 silver badges23 bronze badges asked Jul 26, 2020 at 5:54 nareshnaresh 1
Add a comment  | 

1 Answer 1

Reset to default 0

Wordpress taxonomies (Categories and Tags) do not have featured images, so there is nothing 'standard' that you can use. Therefore you need to customise your code to suit by using Custom Fields to extend the taxonomy to include an image.

However, manually coding this becomes somewhat more complicated when using Images from your media library so you may find it more convenient to use a plugin to create and manage these custom fields for you. In the link above, you will see three plugins listed at the bottom of the page. I only have experience of Advanced Custom Fields but using that plugin I have added Images to Categories and output them as per your example, so its definitely possible. There are plenty of help pages to assist you with this.

As for showing the empty categories, thats simply a case of adding an extra argument to your get_categories function, like this:

$args = array('parent' => 17,'hide_empty' => false);
$categories = get_categories( $args );
发布评论

评论列表(0)

  1. 暂无评论