Community Forums › Forums › Archived Forums › Design Tips and Tricks › How to display author name only on posts from specific categories
Tagged: .post-info, entry-meta, Magazine Pro
- This topic has 18 replies, 2 voices, and was last updated 6 years, 4 months ago by swann83.
-
AuthorPosts
-
September 4, 2018 at 11:11 am #222927swann83Member
Hi,
I'm using Magazine Pro theme. I've customized the post info so I have [post_date], [post_comments] and [post_edit] in all categories and posts. Now I'm looking for a way to also display [post_author] in all posts from specific categories. And I want it to be displayed just in the default output of single posts – like this: [post_author], [post_date], [post_comments] and [post_edit] –, not in the homepage.
Thanks,
Swann
September 4, 2018 at 11:23 am #222928Brad DaltonParticipantTry this https://my.studiopress.com/documentation/snippets/entry-header-html5/customize-the-entry-header/
You can use conditional tags to control which pages the code executes.
September 4, 2018 at 12:15 pm #222929swann83MemberYes, thank you.
This is exactly my customization in my theme’s functions.php file:
add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter($post_info) {
$post_info = '[post_date] [post_comments] [post_edit]';
return $post_info;
}I've tried to add this ("f1" is the category whose posts I want the author name to be displayed in) but it doesn't work:
add_filter( 'genesis_post_info', 'post_info_filter' );
if ( is_category('f1') ) {
$post_info = '[post_author] [post_date] [post_comments] [post_edit]';
return $post_info;
}I'm not sure I'm good enough to use conditional tags but anyway I don't know if it could work for my purpose (I want author name not to be displayed in the homepage, above excerpts, but just in the single post).
Thanks.
September 5, 2018 at 2:57 am #222936Brad DaltonParticipantTry in_category which is different to is_category
https://codex.wordpress.org/Conditional_Tags#A_Category_Page
September 5, 2018 at 7:00 am #222943swann83MemberUnfortunately, it doesn't work:
add_filter( 'genesis_post_info', 'post_info_filter' );
if ( in_category('f1') ) {
$post_info = '[post_author] [post_date] [post_comments] [post_edit]';
return $post_info;
}Any idea what I'm doing wrong here?
Thanks.
September 5, 2018 at 7:50 am #222945Brad DaltonParticipantThat should add the author on posts in category f1.
However, it may not work if you have a plugin active which uses the same hook ( Like Genesis Simple Edits ) or other conflicting code.
September 5, 2018 at 7:57 am #222946swann83MemberI have Simple Hooks, that is very important for my site. Is it creating conflicts? How can I solve them?
September 5, 2018 at 11:22 am #222958Brad DaltonParticipantBased on my testing, this code works:
add_filter( 'genesis_post_info', 'post_info_filter' ); function post_info_filter( $post_info ) { if ( in_category('f1') ) { $post_info = '[post_author]'; return $post_info; } }
September 6, 2018 at 3:05 am #222994swann83MemberOk, it works only if I declare it once. If I use both of these codes then an error occurs: Cannot redeclare post_info_filter() (previously declared in....
add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter($post_info) {
$post_info = '[post_date] [post_comments] [post_edit]';
return $post_info;
}add_filter( 'genesis_post_info', 'post_info_filter' );
function post_info_filter($post_info) {if ( in_category('f1') ) {
$post_info = '[post_author] [post_date] [post_comments] [post_edit]';
return $post_info;
}}
As I said I need [post_date] + [post_comments] + [post_edit] to be displayed in all categories and posts, by default, and also [post_author] in all posts from only two categories (F1 and News). And by the way, I need that info (post author) to be displayed just in the default output of single posts, not visible below the excerpts in the homepage.
Thanks,
Swann
September 6, 2018 at 3:53 am #222998Brad DaltonParticipantYou can use a else if statement.
Example :
if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
Or like this :
if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true; } else { code to be executed if all conditions are false; }
Or a ternary operator examples
September 6, 2018 at 5:03 am #223002swann83MemberGreat! It works.
What can I do to keep only author name from being displayed in excerpts in the homepage? I want to display it just in the single post view.
Thanks,
Swann
September 6, 2018 at 5:12 am #223004Brad DaltonParticipantSeptember 6, 2018 at 6:08 am #223006September 6, 2018 at 6:11 am #223007Brad DaltonParticipantSeptember 6, 2018 at 6:24 am #223008swann83MemberSorry, how do I do that? The first method seems to be fine.
add_filter( 'genesis_post_info', 'post_info_filter' ); function post_info_filter($post_info) { if ( in_category( array( 10,2640,4678,7084 ) ) ) { $post_info = 'di <em>[post_author]</em> <br/> [post_date] [post_comments] [post_edit]'; return $post_info; } else { $post_info = '[post_date] [post_comments] [post_edit]'; return $post_info; } }
I just want the author name not to be visible in the homepage.
Thanks.
September 6, 2018 at 8:30 am #223011Brad DaltonParticipantI wrote this for you https://wpsites.net/web-design/conditional-post-info/
You should be at a stage where you can work out the conditionals and use the following method :
if (condition) { code to be executed if this condition is true; } elseif (condition) { code to be executed if this condition is true; } else { code to be executed if all conditions are false; }
Some work on this is required at your end.
September 6, 2018 at 10:18 am #223016swann83MemberThank you, braddalton. I appreciate it.
I'm sorry my English isn't good enough. I haven't been able to make it quite clear that I have very limited technical knowledge. I'm fairly satisfied with the current configuration, but it would be best if I could display the entry meta with authors name on single posts only (from those categories) and the standard entry meta without authors name (only date, comments and edit) in the entry header, including front and blog pages.
Thanks,
Swann
September 6, 2018 at 10:59 am #223019Brad DaltonParticipantSeptember 6, 2018 at 12:28 pm #223021swann83MemberOk, I wrote this and it works.
add_filter( 'genesis_post_info', 'post_info_filter' ); function post_info_filter($post_info) { if ( in_category( array( 10,2640,4678,7084 ) ) AND ( is_single() ) ) { $post_info = 'di <em>[post_author]</em> <br/> [post_date] [post_comments] [post_edit]'; } else { $post_info = '[post_date] [post_comments] [post_edit]'; } return $post_info; }
Thank you so much for your time, braddalton.
Swann.
-
AuthorPosts
- The topic ‘How to display author name only on posts from specific categories’ is closed to new replies.