Site logo

Archived topic

adding comments and count to meta header in element

9 replies · Started by Sneaky on July 26, 2022

Viewing posts 1–10 of 10

In the customiser blog settings for single post pages there is no option to add the comments count in the meta under the title. So I am using an Element to create and display the meta for posts under the title in my styles. I am able to get author, date and category, but not the comments meta.

So I am having Date | Categories | (Comments) | Author

Is there some code I can add to my Element for the post meta so it will display the comments link with the number of comments?

Thanks

hmmmm I don't see Dynamic Data feature? is there something I need to turn on/off?

What Element are you using? It’s available for Block Elements. Through a GenerateBlocks Headline Block in your case, to be specific.

ok, I don't use GenerateBlocks :(

I use an alternative (Kadence) as it has the other block features I need that GenerateBlocks doesn't have.

Any other way I can get the comments (1) link in the meta?

May I suggest adding it to the customiser for single pages. It is there for Archives so why not Single? Though I still prefer to add it in an Element

I see. We can add it through code.

What Element are you using for the post meta? Is it a Block Element or a Hook Element?

Block :)

Try this PHP snippet:

function add_comments($atts, $content = null) {
      ob_start();
      do_action('comments_number');
      return ob_get_clean();
}
add_shortcode('get_comments_num', 'add_comments');

add_action('comments_number', function(){
	echo '<p>' . get_comments_number() . '</p>';
},  10);

Adding PHP reference: https://docs.generatepress.com/article/adding-php/#code-snippets

With this snippet, you can use shortcode [get_comments_num] through a WP shortcode Block to output the comment count of a post.

Excellent, thank you, I have just edited the output and it works a treat :)

I will now make it a link to the comments :)

Cheers

You’re welcome Sneaky! :)

This archived topic is closed to new replies.