繁体   English   中英

WordPress:获取最新帖子,不包括当前帖子

[英]Wordpress: Get recent posts, exclude current post

我试图获取最近的帖子,仅在当前帖子的类别中,而排除当前帖子,但我无法使它正常工作:

$curr_cat = get_the_category();

$args = array( 'numberposts' => '10', 'post_status' => 'publish', 'category' => $curr_cat['0']->cat_ID, 'exclude' => $post->ID );
$recent_posts = wp_get_recent_posts( $args );

只是一遍又一遍地显示当前帖子。

约翰,您可以尝试以下类似的代码,我不知道您的情况,但是在我的一个项目中对我有用

$args = array ('category__in' =>  $curr_cat['0']->cat_ID, 'posts_per_page' => 10, 'post__not_in' => array( $post->ID ) );

您的代码很好,请确保您正在回显您的帖子,例如:

foreach( $recent_posts as $recent ){
    echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' .   $recent["post_title"].'</a> </li> ';
}

如果执行上述操作,请确保已设置$post ,并且您位于类别存档或单个发布文件中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM