我有一个页面,我想在其中引用特定帖子的标题。这是我现在带有循环的代码-
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'cat' => '3',
);
$product_posts = get_posts( $args );?>
<p>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php echo get_the_title(); ?>
</p>
<?php endforeach; ?>
我不想遍历所有帖子,我希望能够挑选出某些帖子。例如,在我有<p> get_the_title </p>
的地方,我希望能够将其显示为-
<p>Title of Post 5 vs Title of Post 6</p>
我该怎么做?
答案 0 :(得分:1)
您可以尝试以下操作:
在 $ catquery 查询中, cat = 3 是类别ID,因此您可以使用特定的类别ID进行更改。并且 post_per_page = 5 是帖子总数,因此您也可以根据需要进行更改。
<?php $catquery = new WP_Query( 'cat=3&posts_per_page=5' ); ?>
<?php while($catquery->have_posts()) : $catquery->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>
谢谢,让我知道是否有任何疑问。
答案 1 :(得分:0)
尝试使用帖子ID
$postid= array(144, 246);
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'post__in' => $postid,
'posts_per_page'= 5
);
// The Query
$the_query = new WP_Query( $args );
<?php while($the_query->have_posts()) : $the_query->the_post(); ?>
<p><?php the_title(); ?></p>
<?php endwhile; wp_reset_postdata(); ?>