hide_empty is ignored if category list is called multiple times
-
Hello,
Sorry if this is a known issue.If the category list is called multiple times on the same page, hide_empty is ignored.
This problem occurs when there is a category to which a post not included.Environment.
WordPress: 6.0;
Language: Japanese;This phenomenon occurs starting with 6.0.
It did not occur in 5.0.For example, the following code
function my_category(){ $args = array( 'title_li' => '', ); return wp_list_categories($args); } echo my_category(); echo my_category();
This problem occurs with all of get_terms(), get_the_category(), and wp_list_categories().
It occurs when called multiple times within the same page, even if the template is divided into header.php, footer.php, etc.My expected behavior is for hide_empty to work no matter how many times it is called within the same page.
My workaround is the following code.function my_function() { $terms_cache = get_transient('my_list'); if($terms_cache){ return $terms_cache; } $args = array( 'title_li' => '', 'echo' => 0, 'taxonomy' => 'category', ); $terms = wp_list_categories($args); if(is_wp_error($terms) || !($terms)) return ''; set_transient( 'my_list', $terms, 1 * HOUR_IN_SECONDS ); return $terms; }
It is executed using transient.
This method works as expected.Please confirm this. Thank you.
- The topic ‘hide_empty is ignored if category list is called multiple times’ is closed to new replies.