I tried to show all the category name together with this code. But it shows me only one category (uncategorized). I want to show category names like - category 1, category 2, category 3 and so on. Can anyone help to find the problem?
<?php
$categories = get_categories();
if (!empty( $categories)) {
foreach( $categories as $category ) {
echo ''<span class="simple-news-categories">' . $category->name . '</span>';
}
}
?>
CodePudding user response:
get_categories()
won't return all the categories. It will return only that category which has post associated with it. Which means, the function will return only the categories, that have been used in the post. So you need to set the argument hide_empty
to false.
Try this
$categories = get_categories( array('hide_empty' => false));
$categories = wp_list_pluck($categories, 'name');
echo '<span >'.implode(', ', $categories).'</span>';
UPDATE
If your requirement is to show WooCommerce product categories, then use the following.
$categories = get_categories( array('hide_empty' => false, 'taxonomy' => 'product_cat'));
$categories = wp_list_pluck($categories, 'name');
echo '<span >'.implode(', ', $categories).'</span>';