2017-02-16 70 views
0

我在category.php此代碼,我要顯示在其中,我的類別,「$cat」,並提到id 63類我寫的東西,但不想工作職位:/WordPress的獲取類ID數組

<?php 
$cat->term_id; 
$my_query_args = array(
    'posts_per_page' => 6, 
    'tax_query' => array(
     array(
      'taxonomy' => 'category', 
      'field' => 'id', 
      'terms' => array(63, $cat), 
      'operator' => 'AND' 
     ) 
    ) 
); 
$my_query = new WP_Query($my_query_args); 
if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); 

     ?> 
     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
    <?php 
    endwhile; 
endif; 
wp_reset_postdata(); 

?> 

有人能告訴我爲什麼不招致當前顯示的類別$cat

在此先感謝您的幫助!

回答

0

可能你的$cat變量是一個對象。嘗試改變'terms' => array(63, $cat),'terms' => array(63, $cat->ID),'terms' => array(63, $cat->id),

1
$category = get_category(get_query_var('cat')); 
echo $cat_id = $category->cat_ID; 

這會給你當前類別ID。 現在把下面的查詢WordPress的循環開始前在category.php

query_posts('cat='.$cat_id); 

如果你不正常,多數民衆贊成類想讓它複雜與參數過多。這將通過檢查您當前的類別ID來動態工作。

1

可能你會在你的tac_query中混入'field'參數。

'field' => 'term_id', 

field-Select taxonomy term by。可能的值是'term_id','name','slug'或'term_taxonomy_id'。默認值是'term_id'。

0

試試這個設置你的貓的ID或名字在貓= ..

<?php 
$catquery = new WP_Query('cat=3&posts_per_page=10'); 
while($catquery->have_posts()) : $catquery->the_post(); 
?> 
<ul> 
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3> 

<ul><li><?php the_content(); ?></li> 
</ul> 
</li> 
</ul> 
<?php endwhile; ?>