【问题标题】:How to filter Wordpress posts using a hook in a plugin?如何使用插件中的钩子过滤 Wordpress 帖子?
【发布时间】:2011-02-11 20:35:52
【问题描述】:

我正在创建一个 Wordpress 插件,作为这个平台上开发的新手,我被这个问题困住了。

我希望循环中的帖子按类别过滤,由用户通过管理页面定义。我实际上希望能够修改插件中的 query_post() 参数,但我发现的唯一技巧是使用我的用户定义的标准重新运行 query_post(),这是我想避免的。

另外,由于插件的性质,我认为修改主题的模板是没有意义的。

我确定解决方案很明显,但找不到!

【问题讨论】:

    标签: wordpress post filter plugins categories


    【解决方案1】:

    我认为有更好的解决方案,但这就是我最终解决的方法:

    add_filter ( 'query_vars', 'myplugin_filter_posts');
    
    function myplugin_filter_posts( $content )
    {
      //WP's query handler
      global $wp_query;
    
      //The id of the category whose posts I'd like to show
      $catId = 1;
    
      $result = $wp_query->query( 'cat='.$catId );
      return $content;
    }
    

    如果您提示更好的解决方案,请分享:)

    【讨论】:

      猜你喜欢
      • 2017-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-12
      • 1970-01-01
      • 2013-12-03
      • 2012-08-19
      • 1970-01-01
      相关资源
      最近更新 更多