您需要的是设置show_option_none 添加到易于识别的字符串,然后添加wp_list_categories 仅当内容不包含该字符串时。。
所以你的foreach循环应该看起来像
foreach( $taxonomies as $taxonomy ) {
$args = array(
\'orderby\' => \'name\',
\'echo\' => false,
\'taxonomy\' => $taxonomy->name,
\'title_li\' => \'<span class="taxonomy_title">\' . __( $taxonomy->labels->name, \'your-themes-text-domain\' ) . \'</span>\',
\'show_option_none\' => \'%%NOCAT%%\'
);
$list = wp_list_categories( $args );
$empty = (bool) substr_count( strip_tags($list), \'%%NOCAT%%\');
$content .= ! $empty ? \'<ul>\' . $list . \'</ul>\' : \'\';
}
这样,如果您的海关税没有条款
wp_list_categories 未添加到html输出。
几天前,我遇到了一个问题,比如
add_filter( \'the_content\', \'display_post_taxonomies\' );
因为一些SEO插件使用
get_the_excerpt 要在
<head> 页面的节。问题是
get_the_excerpt, 如果没有post的手动摘录,请致电
wp_trim_excerpt 这个函数启动
the_content 钩结果:您将在页面的头部找到分类法列表:这太糟糕了。
您还应该注意\'the_content\' 钩子不仅可以在主查询中触发,所以如果您在侧栏或页脚上有一些辅助查询,您的函数将在那里再次调用:这很糟糕。
我建议你
if( is_single() ) {
替换为
if( is_single() && did_action(\'loop_start\') ) {
此阻止功能在
<head> 部分
然后,在返回内容之前删除过滤器,这样可以确保过滤器运行一次,因此
return $content;
成为
remove_filter( \'the_content\', \'display_post_taxonomies\' );
return $content;