2015-06-27 265 views
0

我試圖讓WP功能正常工作。在子類別頁面上顯示wordpress子類別帖子

我希望它顯示來自所選子類別頁面的帖子,例如,當導航到:www.example.com/category/fruits/apples/時,顯示「蘋果」類別下的所有自定義帖子。我想動態地做到這一點,所以無論子類別(蘋果,橙子,梨等)的數量如何,每次訪問子類別頁面時都會起作用。

以下是我目前的功能,但我不知道get_query_var('cat')是否正確實施。目前,當我訪問子類別頁面時,它會顯示父類別爲「fruits」的所有帖子,但我希望它僅顯示「蘋果」帖子。

<?php 

$cat = get_query_var('cat'); // get current category 
$yourcat = get_category($cat); 

// only display product CPT posts 
query_posts(array('post_type' => 'products')); 

if (have_posts()) : while (have_posts()) : the_post(); 
?> 
    <div class="col-sm-3"> 
    <div class="thumbnail"> 
     <div class="more"><a href="<?php the_permalink(); ?>"><span class="fa fa-location-arrow"></span></a></div> 
     <?php the_post_thumbnail(); ?> 
     <div class="caption"> 
      <a href="<?php the_permalink(); ?>" class="btn btn-default" role="button"><?php the_title(); ?></a> 
     </div> 
     </div> 
    </div> 
<?php endwhile; endif; wp_reset_query(); ?> 
+0

我想我已經設法解決了這個問題!我對查詢帖子並不熟悉,但在查看了這些代碼後,發現了我正在尋找的內容。這是更新的代碼位: '$ category = get_category(get_query_var('cat')); $ cat = $ category-> cat_name; //只顯示產品CPT帖子 query_posts(array('post_type'=>'products','category_name'=> $ cat));' –

+0

幹得好。將調整後的代碼放入答案中,並將其標記爲已解決。這將使人們更容易找出未解決的問題。 – RST

回答

0

將$ cat變量添加到query_posts數組中以獲取當前選定的類別帖子。

$category = get_category(get_query_var('cat')); 
$cat = $category->cat_name; 

// only display product CPT 
posts query_posts(array('post_type' => 'products', 'category_name' => $cat)); 
相關問題