-
jrmora
Hello, friends
I’m still testing GenerateBlocks Beta and I have a couple of questions:1 – Is there any way to stop (Or add a specific message) the ‘No comments’ field from appearing only on posts that have closed comments with a filter or something? The only two options now are default and adding a custom general text for all. I hope I’ve explained
2- Would using an element with this data instead of the native method have any impact on performance?
Tnx!
-
Hi there,
1. I’m not sure I understand what you mean by closed comments, do you mean you want to hide the no comments when there’s no comment on the post?
2. No, there’s no performance impact.
-
jrmora
Hi again Ying
1 -I’m referring to the native WordPress option to close comments, either on a specific post or after x amount of time.
2- Great
-
So you only want it to hide when the comment is closed?
and you do want it to show “no comments” when the comment is open?
-
jrmora
Yes, exactly, but only show the link when comments are closed if it had some comments before closing and hide it if it had no comments.
-
Alvind
Hi there,
Could you try adding this snippet:
add_filter('render_block', function ($block_content, $block) { if (! isset($block['attrs']['className']) || strpos($block['attrs']['className'], 'hide-no-comment') === false) { return $block_content; } global $post; $post_id = $post->ID; // If there are no comments, return empty to hide the block if (get_comments_number($post_id) === 0) { return ''; } return $block_content; }, 10, 2);
Then, on the block with the comment count tag, add a class of
hide-no-comment
under Advanced > Additional CSS Classes.
Adding PHP: https://docs.generatepress.com/article/adding-php/ -
jrmora
Hi, @Alvind
Thank you very much.
I just tried it in staging and it didn’t seem to work, do you think the block needs to be re-rendered or something? -
Hi there,
you could try this:
add_filter('comments_number', function($output) { if (!comments_open()) { $output = 'Comments are closed'; } return $output; });
And don’t set any specific comment text in the dynamic data options.
-
jrmora
Hi, David
I put this new filter in the functions.php and no specific comment in the dynamic data options.He still does not hide it.
It only shows the “Comments are closed” message where there are comments, but does not hide link it where there are none.
As for example in this one with closed comments and no comments:
https://staging.jrmora.com/test-generateblocks-2-0-alpha-2/I have left login details in case you want or need to look something up.
-
Try this instead, and make sure the
hide-no-comment
class is added to the text block.add_filter( 'render_block', function ( $block_content, $block ) { // Check if the block has attributes and className if ( ! empty( $block['attrs']['className'] ) && strpos( $block['attrs']['className'], 'hide-no-comment' ) !== false ) { // Check if comments are closed and no comments exist if ( ! comments_open() && get_comments_number() < 1 ) { $block_content = ''; // Hide the block } } return $block_content; }, 20, 2 );
-
jrmora
Good evening Ying.
With this filter it hides the whole text block when comments are closed 🙁 -
With this filter it hides the whole text block when comments are closed 🙁
Isn’t that what you want?
Can you explain some more about what you want?
And can you share 4 post links that are:
1. comments open, has comments.
2. comments open, no comments.
3. comments closed, no comments.
4. comments closed, has comments.And let me know what you want for the 4 kinds of posts exactly.
-
jrmora
Well, I may not have explained it well because of the language. I will try as you propose.
Scenario:
An element to be displayed in posts with a GB Text block with three dynamic fields: date, post date modified and comments count.
Code:
<!-- wp:generateblocks/text {"uniqueId":"b2b6b3a7","tagName":"p","styles":{"fontWeight":"normal","fontSize":"14px"},"css":".gb-text-b2b6b3a7{font-size:14px;font-weight:normal}"} --> <p class="gb-text gb-text-b2b6b3a7">GB 2.0 Element -Publicado: {{post_date}} | Actualizado: {{post_date type:modified}} | {{comments_count link:comments}}</p> <!-- /wp:generateblocks/text -->
And what is intended is:
1. comments open, has comments (Normal behaviour, shows link with number of comments)
https://staging.jrmora.com/1-comments-open-has-comments/2. comments open, no comments (Normal behaviour, shows link “Comment” or “No comments” etc).
https://staging.jrmora.com/2-comments-open-no-comments/3. comments closed, no comments. (Only the comments link count field is hidden)
https://staging.jrmora.com/3-comments-closed-no-comments/4. comments closed, has comments. (Normal behaviour, shows link with number of comments)
https://staging.jrmora.com/4-comments-closed-has-comments/ -
An element to be displayed in posts with a GB Text block with three dynamic fields: date, post date modified and comments count.
Use a separated text block for the comments count, then the code should work.
-
jrmora
OK, I’ll try to put the separate Text block in a container with two grids. Let’s see if I can square it.
You’re referring to your last code, right?
https://generate.support/topic/comments-field-in-element-gb-2-0/#post-148082
-
Yes, that’s right 🙂
You don’t have to use Grid block for the layout, just add another text block, and set both text blocks’
display
toinline-block.
- You must be logged in to reply to this topic.