我的分类 - $ taxonomy.php页面的帖子不显示?

问题描述:

我创建了一个自定义的post分类法。现在我想通过特定的分类法显示所有特定的帖子。所以我创建了taxonomy-product_cat.php页面。

这里是产品页面获得长期和链接代码 -

<div class="side_box side_box_1 red5"> 
    <h5><a href="#" class="tgl_btn">Filter Products</a></h5> 
    <h6>Brand</h6> 
    <?php $topics = get_terms('product_cat'); 
     echo '<ul class="advanced-filters tgl_c">'; 
     foreach ($topics as $topic) { 
      echo '<li class="advanced-filter" data-group="Brand"><a href="'.get_term_link($topic).'">'.$topic->name.'</a></li>'; 
     } 
     echo '</ul>';?> 
</div> 

这里自定义后的分类代码---

function product_taxonomy() { 
register_taxonomy(
    'product_cat', //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
    'product',     //post type name 
    array(
     'hierarchical'   => true, 
     'label'     => 'product Category', //Display name 
     'query_var'    => true, 
     'show_admin_column' => true, 
     'rewrite'    => array(
      'slug'    => 'product-category', // This controls the base slug that will display before each term 
      'with_front'  => false // Don't display the category base before 
      ) 
     ) 
); 
} 
add_action('init', 'product_taxonomy'); 

这里是分类学product_cat.php页面代码 -

<?php if (have_posts()) : while (have_posts()) : the_post(); $unique = "_product_"; ?> 
    <div class="col-md-3 col-xs-6 element mb30"> 
    <div class="main_box"> 
     <div class="box_1"> 
     <div class="product-image"> 
      <a href="<?php the_permalink(); ?>"> 
      <?php the_post_thumbnail($post->ID, 'product-image',array('class' => 'img-responsive'));?> 
      </a> 
     </div> 
     <div class="overlay hidden-sm hidden-xs"> 
      <a href="<?php the_permalink(); ?>" class="btn_c more_btn">More Info</a> 
     </div> 
     </div> 
     <div class="desc"> 
     <h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5> 
     <p><?php echo get_post_meta(get_the_ID(),$unique.'brand_name',true); ?></p> 
     </div> 
    </div> 
    </div> 
    <?php endwhile; ?> 
    <?php else :?> 
    <h3><?php _e('Not Found Any Product.'); ?></h3> 
<?php endif ?> 

但结果是没有发现任何Product.So请帮助我的人怎么能解决这个problem.Thanks

看到文档

https://developer.wordpress.org/reference/functions/get_terms/

具体

由于4.5.0,分类应当通过 '分类法' 的说法$ args数组中传递:

$术语= get_terms(array( 'taxonomy'=>'post_tag', 'hide_empty'=> false, ));

而不是第一个参数是分类法。

get_terms('product_cat'); 

你应该做

get_terms(array(
    'taxonomy' => 'product_cat' 
)); 

不知道如果是这样的问题,但似乎是最明显的事情。

UPDATE

你尝试这个

get_terms(array(
    'taxonomy' => 'product_cat', 
    'hide_empty' => 0 
)); 

这将显示分类,如果你不与

wp_insert_term() 

插入任何实际条件按该页面, https://wordpress.org/support/topic/get_terms-does-not-return-my-custom-taxonomy-terms

我通常不会在这个级别上使用WP,但我的谷歌技能是无与伦比的......大声笑

+0

你的意思是'不工作'?在那种情况下,你确定那个代码正在运行,你可以在那里回应一些100%确定的代码。 'function product_taxonomy(){echo'HELLO'..'可以在调用'get_terms'之后运行。鸡和鸡蛋,如果没有先执行,就不算数。 – ArtisticPhoenix

+0

你插入任何条款,'wp_insert_term();' – ArtisticPhoenix

+0

我的意思是不工作..是我插入产品的terms.but不显示当我点击条款链接。顺便说一句,分类模板正在工作,因为它的返回值'未找到任何产品'。 – Romimitu