I just try to add some content at the bottom of each posts from a
specific category but the content is added in all posts.
Are you sure your “Hello World!” text is added in all posts? Because is_single() && is_category()
would never be true because a query can’t be for a single post and a category archive at the same time. 🤔
So if you want to check if the current (or a specific) post is within a specific category, you would instead use in_category()
like so:
function bottom_post_message() {
// Check if the current request/URL is for a single post, and that the post is
// in any of the categories passed to in_category().
if ( is_single() && in_category( array( 3, 5 ) ) ) {
// your code
}
}
And note that you incorrectly used is_category()
— you should pass the category IDs and not an instance of WP_Query
.
See the documentation for is_single()
and is_category()
for more details about the functions (what they do, correct syntax, etc.).