分类术语的打印图标

Print icon for taxonomy terms

在我的 wordpress 主题中 Taxonomy = my-category & 类别是 AB,AC,AD 等自定义 post 类型。

我想为自定义类别打印图标,下面 index.php 上的 wp_query 中使用 if & else 语句是我的代码。

 $args = array(
 'post_type'      => 'mycpt',
 'posts_per_page' => 10,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
  while( $query->have_posts() ) {
    $query->the_post(); 
    the_content();

if ($term_id === 10){
    echo '<p class="icon-gear"></p>';
}
else if ($term_id === 11){
    echo '<p class="icon-star"></p>';
}
else if ($term_id === 12){
    echo '<p class="icon-tv"></p>';
}   
 //and continuous  ..............
}
}

CSS

 .icon-gear:before { background:url('gear.svg') no-repeat; }
 .icon-star:before { background:url('star.svg') no-repeat; }
 .icon-tv:before { background:url('tv.svg') no-repeat; }

如何打印分类类别的 svg 图标?

我认为这是错误的处理方式并且不可维护。

就个人而言,我会考虑使用自定义字段将文本字段添加到您的类别中,您可以通过仪表板为每个类别设置图标。然后你只需要在你的循环中回显一个自定义字段,如果它是空的,可以默认为 blank/default 图标。

如果您还没有使用高级自定义字段插件,请查看它。

测试代码,试试这个

 $args = array(
 'post_type'      => 'mycpt',
 'posts_per_page' => 10,
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
  while( $query->have_posts() ) {
    $query->the_post(); 
    the_content();

$terms = get_the_terms( $post->ID, 'my-category');
foreach ( $terms as $term ) {
    $termID[] = $term->term_id;
}
if ($termID[0] === 10){
    echo '<p class="icon-gear"></p>';
}
else if ($termID[0] === 11){
    echo '<p class="icon-star"></p>';
}
else if ($termID[0] === 12){
    echo '<p class="icon-tv"></p>';
}   
 //and continuous  ..............
}
}