Php WordPress获取类别ID到数组
我在Php WordPress获取类别ID到数组,php,wordpress,categories,Php,Wordpress,Categories,我在category.php中有这段代码,我想显示我所在类别的帖子,“$cat”以及引用id63的类别。我写了一些东西,但不想工作:/ <?php $cat->term_id; $my_query_args = array( 'posts_per_page' => 6, 'tax_query' => array( array( 'taxonomy' => 'category', 'fie
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
提前感谢您的帮助 您的
$cat
变量可能是一个对象。尝试将'terms'=>数组(63,$cat),
更改为'terms'=>数组(63,$cat->ID),
或'terms'=>数组(63,$cat->ID),
您的$cat
变量可能是一个对象。尝试将'terms'=>数组(63,$cat),
更改为'terms'=>数组(63,$cat->ID),
或'terms'=>数组(63,$cat->ID),
这将为您提供当前类别id。
现在将下面的查询放在category.php中wordpress循环开始之前
query_posts( 'cat='.$cat_id);
如果这是一个普通的类别,你不想让它太复杂,有太多的参数。这将通过检查当前类别id动态工作
这将为您提供当前类别id。
现在将下面的查询放在category.php中wordpress循环开始之前
query_posts( 'cat='.$cat_id);
如果这是一个普通的类别,你不想让它太复杂,有太多的参数。这将通过检查您当前的类别id动态工作。您可能会在tac_查询中弄乱“field”参数
'field' => 'term_id',
字段-按术语选择分类法。可能的值为“术语库id”、“名称”、“slug”或“术语库分类库id”。默认值为“term\u id”。可能是您在tac\u查询中弄乱了“field”参数
'field' => 'term_id',
字段-按术语选择分类法。可能的值为“术语库id”、“名称”、“slug”或“术语库分类库id”。默认值为“term_id”。尝试将您的cat 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; ?>
-
尝试将您的cat 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; ?>
-