在 post 节目上挂钩或过滤
hook or filter on post show
我应该使用什么 hook/filter 才能不在我的博客上显示某些 post(例如,如果 post 是去年写的。)
我想使用挂钩/过滤器而不是模板中的代码,因此插件或 rss 提要无法访问它
You can use pre_get_posts()
action, this hook is called after
the query variable object is created, but before the actual query is
run. So you have use it with different conditional.
示例代码如下:
function wh_getThisYearPost($query)
{
if (($query->is_home()) //<-- for home page
|| $query->is_feed() //for feed
|| $query->is_search() // for search
)
{
//to get post from current year only.
$query->set('year', date('Y'));
}
}
add_action('pre_get_posts', 'wh_getThisYearPost');
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。
请注意:以上代码不适用于您的自定义 WP_Query
您已在这些查询中手动添加 Date Parameters。
希望对您有所帮助!
我应该使用什么 hook/filter 才能不在我的博客上显示某些 post(例如,如果 post 是去年写的。) 我想使用挂钩/过滤器而不是模板中的代码,因此插件或 rss 提要无法访问它
You can use
pre_get_posts()
action, this hook is called after the query variable object is created, but before the actual query is run. So you have use it with different conditional.
示例代码如下:
function wh_getThisYearPost($query)
{
if (($query->is_home()) //<-- for home page
|| $query->is_feed() //for feed
|| $query->is_search() // for search
)
{
//to get post from current year only.
$query->set('year', date('Y'));
}
}
add_action('pre_get_posts', 'wh_getThisYearPost');
代码进入您的活动子主题(或主题)的 function.php 文件。或者在任何插件 php 文件中。
请注意:以上代码不适用于您的自定义 WP_Query
您已在这些查询中手动添加 Date Parameters。
希望对您有所帮助!