Archived topic
Display author name, date, and comment number in a Page Header
9 replies · Started by Paul on October 2, 2017
I'm looking to display the post author name, date, and comment number (with icons) inside a Page Header, exactly like this:

At the moment the way I'm doing that is via a customised version of content-single.php inside my child theme plus a negative margin in the CSS.
In another post Tom mentioned it is possible to do what I am wanting directly in a Page Header, and that he would show me how if I opened a topic - so here it is :)
Hey Paul,
This is what the template tags are for. The only one that doesn't exist is the comments number, but we can create a shortcode for that:
add_shortcode( 'comment_number', 'tu_comment_number_sc' );
function tu_comment_number_sc() {
ob_start();
comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
return ob_get_clean();
}
Then add this inside your Page Header:
<div class="page-header-meta">
<span><i class="your-icon"></i> {{post_author}}</span> <span><i class="your-icon"></i> {{post_date}}</span> <span><i class="your-icon"></i> [comment_number]</span>
</div>
You can learn more about template tags here: https://docs.generatepress.com/article/page-header-template-tags/
Thanks Tom! I've not added it to the site mentioned above yet, but I have tested it on another and it works perfectly :)
Where should I add this to? :
add_shortcode( 'comment_number', 'tu_comment_number_sc' );
function tu_comment_number_sc() {
ob_start();
comments_popup_link( __( 'Leave a comment', 'generatepress' ), __( '1 Comment', 'generatepress' ), __( '% Comments', 'generatepress' ) );
return ob_get_clean();
}
Hi there,
the code is PHP:
https://docs.generatepress.com/article/adding-php/
If you're not using a Child Theme then the Code Snippets that is linked in the article is the easiest way.
Can the comment count output "0" instead of "Leave a comment" when there are no comments?
Ok, I guess, updating Leave a comment to 0 in the function will do!
Hi George,
Have you solved your concern?
If you need further help with this, please open a new topic as it looks like the original topic is resolved. Thank you.
Apologies, I am sorted, thank you!
No problem.:)