如何使用插件中的钩子过滤 WordPress 帖子?

发布于 2024-08-30 20:09:58 字数 231 浏览 2 评论 0原文

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

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

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

我确信解决方案是显而易见的,但找不到它!

I'm creating a Wordpress plugin and, being a newbie in the development on this platform, I'm stuck on this problem.

I'd like to have posts in the loop filtered by categories, defined by the user through an admin page. I would actually like to be able to modify query_post() parameters in the plugin, but the only trick I found is to re-run the query_post() with my user-defined criteria, thing that I would like to avoid.

Also, due to the plugin nature, I think it make no sense to modify the theme's template.

I'm sure the solution is evident, but can't find it!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半暖夏伤 2024-09-06 20:09:58

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

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;
}

如果您有更好的解决方案的建议,请分享:)

I thought there was a nicer solution, but this is how I finally solved it:

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;
}

If you tips for a better solution, please share :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文