【问题标题】:How can I show specific posts on a WordPress page?如何在 WordPress 页面上显示特定帖子?
【发布时间】:2020-03-23 01:20:34
【问题描述】:

我有一个页面,我想在其中引用特定帖子的标题。这是我现在的循环代码 -

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

不过,我不想遍历每个帖子,我希望能够挑选出某些帖子。例如,我有&lt;p&gt; get_the_title &lt;/p&gt; 我希望能够像这样显示它 -

<p>Title of Post 5 vs Title of Post 6</p> 

我该怎么做?

【问题讨论】:

  • 您是否尝试将标题存储在变量中?

标签: php wordpress loops


【解决方案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(); ?>

谢谢,如果有任何疑问,请告诉我。

【讨论】:

    【解决方案2】:

    用帖子 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(); ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2012-02-19
      • 2016-04-10
      相关资源
      最近更新 更多