2017-10-16 134 views
0

我创建了一个自定义帖子类型product,对于此CPT,我还创建了一个名为products_types的分类。wordpress循环中自定义帖子类型的WordPress回声分类

现在在我的所有products的概述页面上,我想回显给出的产品类型。但我不断收到bool(false)

我的代码:

<div class="row"> 
     <?php 
     $loop = new WP_Query(array('post_type' => 'product')); 
     if ($loop->have_posts()) : 
      while ($loop->have_posts()) : $loop->the_post(); 
       ?> 

       <div class="col-md-4 col-lg-3 work"> 
        <div class="category"> 
         <?php 
         $category = get_the_terms('product', 'products_types'); 
         var_dump($category); 
         echo $category; 
         ?> 
        </div> 
        <a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt=""> 
         <div class="overlay"> 
          <div class="overlay-caption"> 
           <p><?php echo the_title() ?></p> 
          </div> 
         </div> 
        </a> 
       </div> 

       <?php 
      endwhile; 
     endif; 
     wp_reset_postdata(); 
     ?> 
</div> 

任何人都可以帮助我在这里吗?

回答

1

您需要在第一个参数get_the_terms()中传递发布ID或对象。使用get_the_ID()返回帖子ID。

例子:

foreach (get_the_terms(get_the_ID(), 'products_types') as $cat) { 
    echo $cat->name; 
} 
0

如何打印自定义后类型的分类术语在WordPress循环?

<div class="row"> 
     <?php 
     $loop = new WP_Query(array('post_type' => 'product')); 
     if ($loop->have_posts()) : 
      while ($loop->have_posts()) : $loop->the_post(); 
       ?> 

       <div class="col-md-4 col-lg-3 work"> 
        <div class="category"> 
         <?php 
         $terms = get_the_terms(get_the_ID(), 'products_types'); 

         if ($terms && ! is_wp_error($terms)) : 

          $category_links = array(); 

          foreach ($terms as $term) { 
           $category_links[] = $term->name; 
          } 

          $categories = join(", ", $category_links); 
          ?> 
          <?php printf(esc_html($categories)); ?> 
         <?php endif; ?> 
        </div> 
        <a href="<?php the_permalink() ?>" class="work-box"> <img src="<?= get_field('image'); ?>" alt=""> 
         <div class="overlay"> 
          <div class="overlay-caption"> 
           <p><?php echo the_title() ?></p> 
          </div> 
         </div> 
        </a> 
       </div> 

       <?php 
      endwhile; 
     endif; 
     wp_reset_postdata(); 
     ?> 
</div> 
相关问题