admin管理员组

文章数量:1330646

$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 );

本文标签: categoriesshow thumbnail for subcategory wordpress