我有一个自定义分类法说"项目类型",这是为自定义帖子"项目"注册的,我有术语"catOne"one_answers"catTwo"。现在我想显示所有的自定义帖子链接到catOne使用"catOne"的term_id,这在我的情况下是9。
所以我能够成功地循环遍历所有的帖子,但它只显示ID,而不是所有的内容。
我的方法:
$cat_id = 9;
$args = array(
'post_type' => 'projects',
'tax_query' => array(
array(
'taxonomy' => 'project-type',
'field' => 'term_id',
'terms' => array( $cat_id )
),
),
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<div id="post-<?php echo $post->ID; ?> <?php post_class(); ?>">
<h1 class="posttitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<div id="post-content">
<?php the_excerpt(); ?>
</div>
</div>
<?php } wp_reset_postdata();
输出
<div id="post-137 class=" ""="">
<h1 class="posttitle"><a href=""></a></h1>
<div id="post-content">
</div>
</div>
<div id="post-135 class=" ""="">
<h1 class="posttitle"><a href=""></a></h1>
<div id="post-content">
</div>
</div>
有人能告诉我我哪里错了吗?
您应该使用$post
对象来获取数据,而不是使用post_class()
, the_permalink()
, the_title()
和the_excerpt()
,就像您使用$post->ID
一样。只有在使用基于have_posts()
的循环时,才应该调用所使用的函数。你不是,所以用:
-
the_permalink()
->get_the_permalink( $post->ID )
-
the_title()
->$post->title
-
the_excerpt()
->$post->post_excerpt
post_class()
-> get_post_class( '', $post->ID )