2016-07-25 83 views
0

我創建了一個自定義的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

回答

0

看到文檔

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

相關問題