dsifjgogw48491752 2013-01-23 21:08
浏览 20
已采纳

如何仅排除某个类别的最新帖子以显示在WordPress的首页上?

I only want the latest post from the category to be excluded from the front page. All others from the same category should be displayed. I can't seem to figure this one out. Here's what I got so far but it excludes the entire category from the front page.

function exclude_category2($query) { 
if ( $query->is_home ) { 
$query->set('cat', '-1,-4,-36'); 
} 
return $query; 
} 
add_filter('pre_get_posts', 'exclude_category2'); 

Thanks for the help!

  • 写回答

1条回答 默认 最新

  • dt4233 2013-01-23 22:04
    关注

    You could use some hook like template_redirect to remove the top element from the global $posts array using array_shift like gok suggests (somewhat, he didn't really say how to do it). In that case it would look like this

    add_action( 'template_redirect', function() {
        global $posts;
        array_shift( $posts );
    });
    

    But I think a more elegant approach would be this

    add_action( 'loop_start', function( $args ) {
        $args[0]->next_post();
    });
    

    If you put this in functions.php, at the beginning of the Wordpress loop, it will automatically go to the next post which means it will skip the first post always.

    If you want to do this only on certain pages, use the relevant template tag like is_home() inside the function. if ( is_home() ) $args[0]->next_post();.

    If you are not using PHP version >= 5.3 you will have to give the function a name, since lower versions of PHP do not support anonymous functions.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度