is_category 在 wordpress 中使用自定义 post 类型的循环中不获取数据

is_category not fetching data inside a loop with custom post type in wordpress

<div class="row sub_content">
    <div class="col-md-12 col-lg-12">
        <div class="dividerHeading">
            <h4><span>Services, that vitsol provide you</span></h4>
        </div>
    </div>
    <?php

         $args = array(

                       'post_type' => 'topvitsolservice',
                       'posts_per_page' => 3
         );

         $vitsol_posts = new WP_Query($args);
         if($vitsol_posts->have_posts() ): while($vitsol_posts-> have_posts() ): $vitsol_posts->the_post();

    ?>
    <div class="col-sm-4">
        <div class="serviceBox_4">
            <div class="service-icon">
              <?php
                        if(is_category('web-designing')){
                            // Some Icon
                            echo '<i class="fa fa-globe"></i>';

                        } else if(is_category('mobile-apps-development')) {
                            // Another Icon
                            echo '<i class="fa fa-icon"></i>';
                        } else {
                           echo '<i class="fa fa-somefallback"></i>';
                        }

              ?>
                <!--<i class="fa fa-signal"></i>-->
            </div>
            <div class="service-content">
                <h3><?php the_title();  ?></h3>
                <p><?php the_excerpt();  ?></p>
            </div>
            <div class="read">
                <a href="" class="btn btn-default">Read info<i class="fa fa-angle-right"></i></a>
            </div>
        </div>
    </div>
<?php

endwhile;
endif;

?>
</div>

你好。我正在使用自定义 post 类型来获取主页中的数据,我还想要带有 is_category 的图标,但它不起作用自定义 post 类型可以正常工作,但循环内的图标带有is_category 没有显示任何结果。

如果您确定您的自定义 post 类型使用的是默认的 WordPress 类别,那么您将能够使用 has_category

if( has_category( 'web-designing' ) ) {
    // do something
}

类别是 WordPress 中的内置分类法之一,但默认情况下它们仅出现在 post 中。您的自定义 Post 类型可能有自己的分类法,在这种情况下,根据 reference,您正在寻找的函数是 has_term 您可以使用它来检查当前的 post在自定义分类中有任何给定的术语。例如以下示例中的 product_cat 分类法。

if( has_term( 'web-designing', 'product_cat' ) ) {
    // do something
}