- This topic has 15 replies, 4 voices, and was last updated 2 years, 6 months ago by David.
-
AuthorPosts
-
September 18, 2021 at 2:55 am #1934485HD
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!
September 18, 2021 at 11:04 am #1934931ElvinStaffCustomer SupportHi 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. 😀
September 18, 2021 at 9:18 pm #1935215HDI have them disabled using Customizing > Layout > Blog
September 18, 2021 at 9:25 pm #1935221ElvinStaffCustomer SupportI 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; }
September 18, 2021 at 11:03 pm #1935277HDThank you, I can see them on the category loop, however not visible on the single blog-post nor main blog loop.
September 18, 2021 at 11:18 pm #1935292ElvinStaffCustomer SupportLet’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' ) )
September 18, 2021 at 11:52 pm #1935312HDThank 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.
September 19, 2021 at 1:21 am #1935351ElvinStaffCustomer SupportTo 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.
September 19, 2021 at 2:09 am #1935377HDThank 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?September 19, 2021 at 2:46 am #1935401ElvinStaffCustomer SupportAh my bad.
Change this part of the code: (line 4)
if( !in_category( 'dogs' ) || )
to this
if( !in_category( 'dogs' ) )
September 19, 2021 at 4:12 am #1935467HDAwesome this works!
All I had to do is reenable
Display author name
underCustomizing > 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!
September 19, 2021 at 10:39 am #1935886LeoStaffCustomer SupportIs 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_PageAlso, 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/September 20, 2021 at 11:08 pm #1937386HDI 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.
September 21, 2021 at 7:57 am #1938068DavidStaffCustomer SupportHi there,
you need to make that an Array eg.
in_category( array( ‘Tropical Birds’, ‘small-mammals’ )
if( !in_category( array('cats' , 'dogs') ))
September 21, 2021 at 8:15 am #1938102HDThank you so much for helping!
-
AuthorPosts
- You must be logged in to reply to this topic.