wordpress wp_get_recent_posts()按ID排除特定帖子

e0bqpujr  于 2023-01-12  发布在  WordPress
关注(0)|答案(3)|浏览(125)

我试图得到两个最近的职位,但我想排除其ID[365]特定职位。有人能帮我吗?这里是我的代码。

$args = array( 'numberposts' => '2' , 'post__not_in' => array( '365' ) );
$recent_posts = wp_get_recent_posts( $args );
 <?php foreach($recent_posts as $post):?>
    <a href="<?php echo $post['guid']?>"><p><?php echo $post['post_title'];?></p></a>
 <?php endforeach;?>
ljo96ir5

ljo96ir51#

只需将您的代码替换为下面提到的代码,您将得到您想要的结果:

<?php $my_args = array('post_type' => 'post' , 'numberposts' => '2' , 'exclude' => '365' );
$my_recent_posts = wp_get_recent_posts( $my_args );?>
 <?php foreach($my_recent_posts as $my_post):?>
    <a href="<?php echo $my_post['guid']?>"><p><?php echo $my_post['post_title'];?></p></a>
 <?php endforeach;?>
djp7away

djp7away2#

您应该再次为该函数使用read the docs。您有一个可用的exclude参数:

$args = array( 'numberposts' => '2' , 'exclude' => 365 );
vnjpjtjt

vnjpjtjt3#

wp_get_recent_posts不支持post__not_in参数,您将需要使用exclude

wp_get_recent_posts的参考链接

<?php
$args = array( 'numberposts' => '2' , 'exclude' => '365' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post){ 
?>
     <a href="<?php echo $post['guid']; ?>">
        <p><?php echo $post['post_title']; ?></p>
     </a>
 <?php } ?>

相关问题