WordPress获取数组的类别ID



我在 category.php中有此代码,我想显示我所在类别的帖子," $cat",而 id 63.我写了一些东西,但不想写一些东西工作:/

<?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

预先感谢您的帮助!

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

这将为您提供当前类别ID。现在,将以下查询放在wordpress循环的启动之前。php

query_posts( 'cat='.$cat_id);

如果那是普通类别,则您不想在太多的args中使其复杂。这将通过检查当前类别ID来动态起作用。

可能是您在tac_query中对'field'参数弄乱。

'field' => 'term_id',

现场 - 选择分类学术语。可能的值是'term_id','name','slug'或'term_taxonomy_id'。默认值是'term_id'。

可能您的$cat变量是对象。尝试将'terms' => array( 63, $cat ),更改为'terms' => array( 63, $cat->ID ),'terms' => array( 63, $cat->id ),

试试此设置您的猫ID或名称cat = ..

<?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; ?>

最新更新