admin管理员组文章数量:1291048
I wanted to use the_content filter to check the post categories. If a post has certain categories selected, depending on the user I want to return a message saying something like 'Sorry, you cannot view this', rather than the content itself.
Having added the filter though, have discovered, the_content filter does not pass the post id or post into the function, so I cannot check the post to see if the conditions are met.
Can anyone suggest a way of doing this, without updating all the page templates?
I wanted to use the_content filter to check the post categories. If a post has certain categories selected, depending on the user I want to return a message saying something like 'Sorry, you cannot view this', rather than the content itself.
Having added the filter though, have discovered, the_content filter does not pass the post id or post into the function, so I cannot check the post to see if the conditions are met.
Can anyone suggest a way of doing this, without updating all the page templates?
Share Improve this question asked Jun 9, 2021 at 8:21 StripyTigerStripyTiger 2771 silver badge6 bronze badges1 Answer
Reset to default 4You'll need to rely on the global post variable, or get_post()
, which is essentially the same thing.
add_filter(
'the_content',
function( $content ) {
$post = get_post();
if ( in_the_loop() && has_category( 123, $post ) ) {
// etc.
}
return $content;
}
);
I included a check for in_the_loop()
, because the the_content
filter is commonly applied outside the loop in contexts where there won't necessarily be a relevant global $post
variable.
本文标签: the contentthecontent filterchecking the post
版权声明:本文标题:the content - the_content filter - checking the post 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741515657a2382872.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论