Archived topic
Display author for selective categories of posts
15 replies · Started by Hardik on September 18, 2021
Hello!
I have disabled the author name output for single posts and the main blog loop page. However, I would like to display the same only for certain categories of posts. How can I do so?
Thanks!
Hi there,
Can you share how you've disabled the author name output on single posts and the blog index page? So we can check and perhaps modify it to add category conditions to it.
Let us know. :D

I have them disabled using Customizing > Layout > Blog
I see.
In that case you'll need this filter.
https://docs.generatepress.com/article/option_generate_blog_settings/
As for usage, here's an example:
Say, you have a category named Cats with category slug "cats" and you want the author to show up on its category archive page.
You can do it with this filter.
add_filter( 'option_generate_blog_settings', 'lh_custom_search_results_page_settings' );
function lh_custom_search_results_page_settings( $options ) {
if ( is_category('cats') ) {
$options['author'] = true;
}
return $options;
}
Example #2: Displaying author on category archive of Dogs (with slug dogs) and posts of the same category.
add_filter( 'option_generate_blog_settings', 'lh_custom_search_results_page_settings' );
function lh_custom_search_results_page_settings( $options ) {
if ( is_category('dogs') && in_category( 'dogs' ) ) {
$options['author'] = true;
$options['single_author'] = true;
}
return $options;
}
Thank you, I can see them on the category loop, however not visible on the single blog-post nor main blog loop.
Let's modify the condition to add the blog index page.
can you change this line - if ( is_category('dogs') && in_category( 'dogs' ) )
to this:
if ( is_home() || is_category('dogs') || in_category( 'dogs' ) )
Thank you, the author is now visible for the specified category on the single-post and main blog loop.
However, all authors are for some reason is visible on the main blog loop.
So let me rephrase my original question a little more clearly: I want authors of all posts belonging to a specified category to be visible on their authored post as well as the main blog loop.
To clarify further:
You mean, an author who doesn't have a post on a specific category shouldn't appear on the archive and blog page?
IF that's the case, we need a completely different filter.
I think what you need is this: https://docs.generatepress.com/article/generate_post_author_output/
Example usage:
add_filter( 'generate_post_author_output', function( $output ) {
// If we're on our "my-post-type" post type, remove the author.
if ( is_home() || is_archive() || is_single() ) {
if( !in_category( 'dogs' ) || ){
return '';
}
}
// Otherwise, display it.
return $output;
} );
What this does is, it checks if the page is the blog page or an archive page or a single post page.
It then checks if the post is in category 'dogs'. If it is NOT 'dogs' category, it returns nothing. else, it will display the author.
Change 'dogs' to the category of preference.
Thank you for your reply. That is exactly what I want to achieve.
However, the code you posted above throws an error. I get syntax error, unexpected ')' on line 4
What should be done here?
Ah my bad.
Change this part of the code: (line 4)
if( !in_category( 'dogs' ) || )
to this if( !in_category( 'dogs' ) )
Awesome this works!
All I had to do is reenable Display author name under Customizing > Layout > Blog Two more queries related to this:
Is there a way to tweak it so I can add another category to it?
Also, is there a way to utilize the 'Elements' feature of Generatepress in order to run arbitrary PHP scripts so as to remove the dependence on the 'Snippets' plugin?
Thank you!
Is there a way to tweak it so I can add another category to it?
Take a look at this for examples:
https://codex.wordpress.org/Conditional_Tags#A_Category_Page
Also, is there a way to utilize the ‘Elements’ feature of Generatepress in order to run arbitrary PHP scripts so as to remove the dependence on the ‘Snippets’ plugin?
No - filters need to be added using one of these methods:
Adding PHP: https://docs.generatepress.com/article/adding-php/
I tried modifying line 4 to this if( !in_category( 'cats' , 'dogs' ) ) by adding a comma, however, that didn't work. I regret I'm not good at coding.
Thank you for understanding.
Hi there,
you need to make that an Array eg.
in_category( array( 'Tropical Birds', 'small-mammals' )
if( !in_category( array('cats' , 'dogs') ))
Thank you so much for helping!