最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Displaying all taxonomies without custom posts?

matteradmin10PV0评论

I have template for displaying my custom taxonomies. But they're display only when there's min. one custom post with one of them. How to do it to display those taxonomies even if there's no posts with them? This is my code:

<div class="c-countires__wrapper">
  <?php $categories = get_categories('taxonomy=instructors_countries&post_type=instructors'); ?>
    <?php foreach ($categories as $category) : ?>
      //content
    <?php endforeach; ?>
</div>

I have template for displaying my custom taxonomies. But they're display only when there's min. one custom post with one of them. How to do it to display those taxonomies even if there's no posts with them? This is my code:

<div class="c-countires__wrapper">
  <?php $categories = get_categories('taxonomy=instructors_countries&post_type=instructors'); ?>
    <?php foreach ($categories as $category) : ?>
      //content
    <?php endforeach; ?>
</div>
Share Improve this question asked Oct 20, 2018 at 16:44 DamianDamian 971 gold badge1 silver badge11 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

This is one of those situations where a helper method might look like the right solution, but brings with it baggage that gets annoying when you try and do serious work with it.

Instead, ignore get_categories, and use the real API one level down that applies to all taxonomies, not just categories.

First, we need to correct a misconception. You are not displaying taxonomies. You are displaying terms in a taxonomy ( a custom taxonomy ). This might sound pedantic, but it's essential to understanding the API. If you don't make this distinction, things will get confusing, even though really they're simple.

  • A taxonomy is a way of grouping/filtering/classifying things, e.g. tag, category, colour, company, type
  • A term is a particular instance of that taxonomy, e.g. red, blue, green, yellow are all terms in the colour taxonomy.

So you are trying to show all the terms in a taxonomy. This is important because if you search for that, you get results. If you search for how to display taxonomies, you get far fewer results.

This leads us to the more general function get_terms, the first example of which answers your question:

$terms = get_terms( array(
    'taxonomy' => 'post_tag',
    'hide_empty' => false,
) );

Like the get_categories function, it returns WP_Term objects. Here's another example from the official docs:

$terms = get_terms( array(
    'taxonomy' => 'my_taxonomy'
) );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    echo '<ul>';
    foreach ( $terms as $term ) {
        echo '<li>' . $term->name . '</li>';
    }
    echo '</ul>';
}

Additional General Notes

  • Your code has an excessive number of <?php and ?> tags. It must takes a long time to type all those unnecessary opening and closing PHP tags. You don't need to close PHP tags at the end of every line. If you have 5 lines of PHP you can get away with a single opening PHP tag, then the 5 lines, then the closing tag. If the last line of your files is PHP, you don't need the closing tag either
  • Notice how the functions use arrays instead of strings for arguments. The old get_categories('taxonomy=instructors_countries&post_type=instructors') style is a tiny bit slower, and prevents you from using more advanced features. For example, it's not possible to do a meta_query or tax_query in WP_Query if you pass it in as a string. Use array( 'taxonomy' => 'instructors_countries' .. etc ) instead. You can even put it across multiple lines for easy reading this way!
  • In your example, you fetch the categories then immediately go into a for loop. But what if no categories were found? Or something else went wrong? In those situations a WP_Error might have been returned. Now you have a PHP error log full of warnings or fatal errors :( `

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far