In the Genesis Slack Group ( #code-snippets channel) Brian Bourn mentioned a new filter in Genesis 2.2 that allows you to customise the comment meta data.
And right there on lines 317 – 333 in the genesis/lib/structure/comments.php file is:
/**
* Allows developer to control whether to print the comment date.
*
* @since 2.2.0
*
* @param boolean $comment_date Whether to print the comment date
* @param string $post_type The current post type
*/
$comment_date = apply_filters( 'genesis_show_comment_date', true, get_post_type() );
if ( $comment_date ) {
printf( '<p %s>', genesis_attr( 'comment-meta' ) );
printf( '<time %s>', genesis_attr( 'comment-time' ) );
printf( '<a href="%s" %s>', esc_url( get_comment_link( $comment->comment_ID ) ), genesis_attr( 'comment-time-link' ) );
echo esc_html( get_comment_date() ) . ' ' . __( 'at', 'genesis' ) . ' ' . esc_html( get_comment_time() );
echo '</a></time></p>';
}
So if $comment_date is set to true, the comment dates are output and if its false, they don\’t.
Therefore to simply remove the comment dates from your comments you can use the WordPress __return_false function and put the following line in your functions file:
// Remove the date and time on comments in Genesis child themes
add_filter( 'genesis_show_comment_date', '__return_false' );
And your comments go from this
to this
(The edit link is showing because I was logged in when I created these screenshots)
The reason I was excited to hear about this new filter is because on my own web design agency\’s website I wanted to remove the time from my comments (and keep the date) but to do so I had use the genesis_comment_list_args filter to use my own custom version of the genesis_html5_comment_callback function. And that\’s quite a few lines of code!
But now with this new filter in Genesis 2.2, there is a neater way:
You can also view this code on github.
add_filter( 'genesis_show_comment_date', 'jmw_show_comment_date_with_link' );
/**
* Show Comment Date with link but without the time
*
* Stop the output of the Genesis core comment dates and outputs comments with date and link only.
* The genesis_show_comment_date filter was introduced in Genesis 2.2 (will not work with older versions)
*
* @author Jo Waltham
* @link http://www.jowaltham.com/customising-comment-date-genesis/
*
* @param boolean $comment_date Whether to print the comment date or not
* @return boolean Whether to print the comment date or not
*/
function jmw_show_comment_date_with_link( $comment_date ) {
printf('<p %s><time %s><a href="%s" %s>%s</a></time></p>',
genesis_attr( 'comment-meta' ),
genesis_attr( 'comment-time' ),
esc_url( get_comment_link( get_comment_ID() ) ),
genesis_attr( 'comment-time-link' ),
esc_html( get_comment_date() )
);
// Return false so that the parent function doesn't output the comment date, time and link
return false;
}
Which makes the comments look like this:
And finally, the reason why this topic came up in the slack channel in the first place is that Paal wanted to show only the date without the link. So one quick further customisation …
add_filter( 'genesis_show_comment_date', 'jmw_show_comment_date_only' );
/**
* Show date on comments without time or link
*
* Stop the output of the Genesis core comment dates and outputs comments with date only
* The genesis_show_comment_date filter was introduced in Genesis 2.2 (will not work with older versions)
*
* @author Jo Waltham
* @link http://www.jowaltham.com/customising-comment-date-genesis/
*
* @param boolean $comment_date Whether to print the comment date or not
* @return boolean Whether to print the comment date or not
*/
function jmw_show_comment_date_only( $comment_date ) {
printf('<p %s><time %s>%s</time></p>',
genesis_attr( 'comment-meta' ),
genesis_attr( 'comment-time' ),
esc_html( get_comment_date() )
);
// Return false so that the parent function doesn't output the comment date, time and link
return false;
}
You get this:
I love Genesis filters π
Please note this filter will only work if you are using Genesis 2.2 or later and a HTML5 theme.
Featured Image: Photo by Ella Olsson on Unsplash
Paal Joachim
Hi Jo
Thank you very much for creating this article!
I will add your last code snippet (and add your article as a resource) to my own article on customizing the comments section in a Genesis child theme.
Have a great weekend!
Jo Waltham
You’re welcome! I just need to implement this on my own site now.
Mike Hemberger
Jo, excellent write up. I saw Brian mention it on Slack but haven’t had time to try. You just saved us some legwork in trying out this filter. Thanks!
Jo Waltham
Thanks Mike.
Sandee
Thank you for your work on this on, Jo! I can see where this will come in very handy for a few projects. Adding this code to my saved snippets, for sure!
Jo Waltham
One of the reasons I wrote it up, so it’s handy for when I need it too!
Davinder Singh Kainth
That’s handy. Thanks for sharing Jo!
Jo Waltham
You’re welcome!
Chinmoy Paul
Hi
I am trying your code in my upcoming theme. I am getting “Notice” error from this line
esc_url( get_comment_link( $comment->comment_ID ) ),
Thanks
Chinmoy
Jo Waltham
Hi Chinmoy
Oops that will be because $comment is not defined in or passed to this function.
As we only need the ID I’ve updated the gist to replace the line
esc_url( get_comment_link( $comment->comment_ID ) ),
with
esc_url( get_comment_link( get_comment_ID() ) ),
Cheers
Jo
Chinmoy Paul
Actually Genesis is already using the global variable. I used that variable. My theme have extra theme options. So I modified your code. But this tut was very helpful for me.
Thank you.
Carlo
Hi Jo. I like the way you changed it to remove the time but keep the date. It should also be noted that this filter only works with HTML5 child themes, even if the parent theme is updated to 2.2.0 or newer.
Jo Waltham
Thanks Carlo, I shall amend the text to point that out.
Hitesh Bhasin
Hello, Please help me. I want to remove the dates altogether for genesis comments. I added the below code but the dates are still showing. my theme is Genesis news theme and it is HTML5 compliant.
add_filter( ‘genesis_show_comment_date’, ‘__return_false’ );
Kindly help
Jo Waltham
Hi Hitesh
Are you using Genesis 2.2 or later? Are you sure your theme is using enabled for HTML5 – ie you are using News Pro theme, not News theme.
Is this issue on your website marketing91.com? Because this website using the XHTML News theme and this filter will not work with this theme as it is not HTML5.
Mike Hemberger
Thanks Jo! Was digging through Genesis core (and somehow missed this filter). I googled for a min and your post came up. Exactly what I needed today. Thanks for being awesome, as usual. π
Jo Waltham
Haha thanks Mike, glad to help.
Regev
You. Are. Awesome.
THANKS!
Shaan Nicol
I was looking to re-order the comment field to have the date before the author, looks like this will do the trick, thanks Jo!
Keller
Thank you so much this worked for me and made my comments styling so much cleaner.