duanmianzhou5353 2019-06-08 13:48
浏览 57

从wordpress中的页面限制几个类别

I have used the 'Recent posts' in Two pages and I need to restrict some posts with specific categories from those two pages. I have added the below code in the functions.php and Saved. But the expected result is not coming. Home page (ID - 12) and Poems Page (ID - 8) are those pages I have added the restriction 772 and 152 are the category ID's of those I want to restrict from the mentioned pages.

function excludeCat($query) {
    $page = get_query_var('paged', 1);

    $exclude = [
        12 => '-772',
        8  => '-152'
    ];

    if ($query->is_home && isset($exclude[$page])) {
         $query->set('cat', $exclude[$page]);
    }
    return $query;
}
add_filter('pre_get_posts', 'excludeCat');

Please advise on the code

  • 写回答

1条回答 默认 最新

  • dongpiao0731 2019-06-25 01:37
    关注

    Just look at the below code and write it in "functions.php" file.It may help you.

    add_filter( 'widget_posts_args', 'my_widget_posts_args');
    function my_widget_posts_args($args) {
            return array(
                'cat' => array(-5,-4) // pass the term id of category which you want to exclude with prefix "-".
                 );
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教