【发布时间】:2018-03-14 13:25:59
【问题描述】:
在我的 wordpress 主题中 Taxonomy = my-category 和类别是 AB,AC,AD 等自定义帖子类型。
我想打印自定义类别的图标,并在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 图标?
【问题讨论】:
-
如果您打算使用类似的 if else,请改用 switch。你也可以通过说
switch ($term_id) case 10: $icon = "star";然后echo "<p class='icon-$icon'";来避免重复代码你能告诉你出了什么问题吗?因为你说的是你想要发生的事情,而不是现在发生的事情。 -
你从哪里得到
$term_id? -
@ikdekker 它没有显示任何东西。没有错误的空框
-
如果您通过查看页面源来检查 html,是否出现了
元素?如果是这样,您可以尝试从 css 中删除 :before。
-
@ikdekker 在页面源中为空。
<p>不显示