PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, October 24, 2022

[FIXED] How to show a category name even when there is no post in Wordpress

 October 24, 2022     categories, wordpress     No comments   

Issue

I am trying to show WordPress Category name in my Taxonomy page, but every time that Category has no post, its name won't be show. How can I show it, even when it's empty?

<?php

 $term = get_queried_object();
 $term->slug;
 $terms = get_the_terms($post->ID, 'productcat');
 $nameTerm = $terms[0]->name;
 $linkTerm = get_term_link($terms[0]);

?>

<span><?php echo $nameTerm ?></span>

Solution

It sounds like you want to get all terms of the category, not just the post terms. Use get_terms. You can pass hide_empty to show the categories that don’t have posts.

<?php
 $term = get_queried_object();
 $term->slug;
 $terms = get_terms( 'product_cat', array(
    'hide_empty' => false,
 ) );
 $nameTerm = $terms[0]->name;
 $linkTerm = get_term_link($terms[0]);
?>
<span><?php echo $nameTerm ?></span>


Answered By - Noah
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing