- This topic has 13 replies, 2 voices, and was last updated 4 years, 8 months ago by
Elvin.
-
AuthorPosts
-
July 19, 2021 at 6:42 pm #1861847
James
Currently I have the following code in my child theme.
add_filter( ‘generate_header_entry_meta_items’, function() {
return array(
‘categories’,
‘tags’,
‘comments-link’,
);
} );That is working fine. But now I want to remove the categories because I am using something else to display the full hierarchy of the categories.
If I remove ‘categories’ from the filter, then the comments-link also disappears from the front end of the site.
add_filter( ‘generate_header_entry_meta_items’, function() {
return array(
//’categories’,
‘tags’,
‘comments-link’,
);
} );How can I display the ‘comments-link’ in entry meta without the ‘categories’?
July 19, 2021 at 10:26 pm #1862072Elvin
StaffCustomer SupportHi there,
That’s strange. Removing that should be as simple as this:
add_filter( 'generate_header_entry_meta_items', function() { return array( 'tags', 'comments-link', ); });Can you try this instead?
add_filter( 'generate_header_entry_meta_items', function( $items ) { return array_diff( $items, array( 'categories' ) ); } );July 20, 2021 at 12:10 am #1862173James
No, that is not working either.
July 20, 2021 at 1:36 am #1862289James
I think I have figured out what is going on here.
The comments-link is only displaying if at least one other item from the filter is set to display in the customizer settings.
With this code.
add_filter( 'generate_header_entry_meta_items', function() { return array( 'categories', 'tags', 'comments-link', ); } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );Then the comment link will only display if at least one of the 2 other filters (tags, categories) is set to display on single posts in the customizer (layout > blog > content > single).
With this code:
add_filter( 'generate_header_entry_meta_items', function() { return array( 'tags', 'comments-link', ); } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );Then the comments link is only shown if tags are set to display in the customizer.
With this code:
add_filter( 'generate_header_entry_meta_items', function() { return array( 'categories', 'comments-link', ); } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );Then the comments link is only shown if categories are set to display in the customizer.
With this code:
add_filter( 'generate_header_entry_meta_items', function() { return array( 'comments-link', ); } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );Then there is no way to display the comments link because there are no other filters used that are enabled in customizer.
I have verified this behavior with a fresh install of WordPress on my local computer with only GP and GP Premium installed with the above codes added.
Actually, my end goal is to only use the comments-link filter by itself (without tag or category, as per the last code snippet above). I am using a custom generate_after_entry_title function to show the author info and published/last updated dates in the entry meta.
July 20, 2021 at 5:00 pm #1863719James
Any update on this?
July 20, 2021 at 6:52 pm #1863771Elvin
StaffCustomer SupportStrange.
On my sandbox site here – https://dev-generate-press.pantheonsite.io/ – You can see that I only have tag and comment link on my entry meta. And all I had to do was to add the first PHP snippet on this reply – https://generatepress.com/forums/topic/show-comments-link-without-category-in-entry-meta/#post-1862072
Do you have multiple instances of the same filter? That could be an issue.
Actually, my end goal is to only use the comments-link filter by itself (without tag or category, as per the last code snippet above). I am using a custom generate_after_entry_title function to show the author info and published/last updated dates in the entry meta.
Having this as an end goal, this should be achievable w/ a snippet as simple as this:
add_filter( 'generate_header_entry_meta_items', function() { return array( 'comments-link', ); } );Not exactly sure why It’s not working on your end but it may be from a multiple instance of the same filter or some other snippet.
If you can provide us temporary backend access, we can try to verify things by checking the backend.
You can provide the details on the private information textfield. Let us know. 🙂
July 20, 2021 at 8:09 pm #1863800James
On your sandbox site, go into the customizer > layout > blog > content > single and uncheck the option to display post tags. Then your comments-link will disappear too!
Or, change your code to the snippet you provided above to only show the comments-link without the tag or category.
add_filter( 'generate_header_entry_meta_items', function() { return array( 'comments-link', ); } );You will see that it doesn’t work. Nothing is displayed.
It only works when something else is included in the filter (category, tag, eg) which is also set to display in the customizer.
Originally, I had the display of tags disabled in customizer, that’s why the code didn’t work. If I enable the display of tags in the customizer, then it works fine, same as your sandbox site.
There is no option in the customizer to enable display of comments-link on single posts. I am guessing there is a bug somewhere that assumes the meta entry is empty if nothing is included in the filter that can be enabled/disabled in customizer.
July 20, 2021 at 10:00 pm #1863886Elvin
StaffCustomer SupportYou can check my sandbox site again.
I’m using the exact same code you’ve provided and the setting on the customizer.
Here’s the setting on the customizer – https://share.getcloudapp.com/9Zudknw0 –
Here’s the active code snippet – https://share.getcloudapp.com/BluK86rz
You can see in this screenshot – https://share.getcloudapp.com/Kou4b2v0 – and on the live site that only the comment link appears.
The purpose of the filter was to override the customizer settings(in cases where you want more specific conditions). It shouldn’t matter what’s set there. 🙂
July 20, 2021 at 11:52 pm #1863992James
The screenshot you sent of your customizer settings is for archives, not single posts.
Actually, your sandbox site is not showing any header meta entry at all on single post view, only in the archive view.
You are missing some code needed to show comments in the header meta on single posts.
add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );The problem I have is with single posts, not archives.
Here are some screenshots of a local site with a fresh install of WordPress and GP Premium and only the following code added.
add_filter( 'generate_header_entry_meta_items', function() { return array( 'categories', 'tags', 'comments-link', ); } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );You can see 3 images in the album linked above. The first image shows the result with tags disabled in the customizer (for single posts). The header entry meta is showing category and comments-link.
The second image shows the result with everything enabled in the customizer. Header entry meta is showing category, tags, and comment-link.
The third image shows the result with tags and categories disabled in the customizer. Now the comments-link is gone too, there is no header entry meta shown at all anymore.
If I change the code to below, then the result is the same as the third screenshot (no header entry meta is shown at all) no matter what is selected in the customizer.
add_filter( 'generate_header_entry_meta_items', function() { return array( 'comments-link', ); } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );The bug is that the comments-link is not displayed in the header entry meta for single posts unless there is something else to display there as well (something else has to be included in the filter and set to display in the customizer).
I hope my explanation is clear now.
July 21, 2021 at 1:10 am #1864087Elvin
StaffCustomer SupportAh, I think you’re right about this one.
I’ve dug around the code and it disables the display of the comment-link by default – https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/post-meta.php#L345-L347
The
add_filter( 'generate_show_comments', '__return_true' );is necessary because of this.You can play around with this –
add_filter( 'generate_header_entry_meta_items', function() { if( is_single() ){ return array( 'comments-link' ) ; } }); add_filter( 'generate_show_comments', function(){ if( is_single() ){ return true; } });See a sample single post page here –
https://dev-generate-press.pantheonsite.io/dignissimos-itaque-suscipit-labore/July 21, 2021 at 11:04 am #1865147James
add_filter( 'generate_show_comments', function(){ if( is_single() ){ return true; } });With this code you cannot enable/disable comments-link on archives using the customizer. I changed it to this.
add_filter( 'generate_show_comments', function(){ return true; });Which seems to be working fine. EDIT – Actually you cannot enable/disable comments-link on customizer with this code either. I guess the customizer is disabled once you add this filter. Anyway, both codes work fine depending if you want to display comments-link in the archive or not.I was just curious why the above codes work, but this one below (taken from other threads of this forum) only works when other items are displayed in the entry meta?
add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );This code snippet appears to do the the exact same thing as the working ones.
July 21, 2021 at 1:18 pm #1865263James
I thought this code was working, but now I notice that it is showing a PHP error on the front end of the site if I view a page instead of a post.
Warning: Invalid argument supplied for foreach() in /var/www/example.com/htdocs/wp-content/themes/generatepress/inc/structure/post-meta.php on line 342I am using the code snippet you provided.
add_filter( 'generate_header_entry_meta_items', function() { if( is_single() ){ return array( 'comments-link' ) ; } }); add_filter( 'generate_show_comments', function(){ if( is_single() ){ return true; } });July 21, 2021 at 1:35 pm #1865283James
Seems to be working now with this code (I think).
add_filter( 'generate_header_entry_meta_items', function() { return array( 'comments-link' ); } ); add_filter( 'generate_show_comments', function(){ if( is_single() ){ return true; } });July 21, 2021 at 8:49 pm #1865612Elvin
StaffCustomer SupportSeems to be working now with this code (I think).
Yes this should work.
You may have to edit
generate_header_entry_meta_itemsto add condition for archive and blog index page if you want a completely different entry meta setup.Thanks for letting us know. Glad you got it sorted. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.