This is a quick post on how to provide your own comments template in your child theme ( overriding comments.php in your parent theme). You can just as easily use the same code to override comments.php if you aren’t using a child theme.
Todo this you need to you need to use the filter ‘comments_template’ ( see the wordpress codex on this filter here)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function greenbox_override_comment_template( $comment_template ) { global $post; if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) { return; } // your full path and name of comments file to use return dirname(__FILE__) ."/my-comments.php"; } add_filter( "comments_template", "greenbox_override_comment_template" ); |
A handy thing you might sometimes want todo is to have a custom comments template, for various custom post types. If you did want todo this you could just add in an if to encase the returned path and filename. e.g.
1 2 3 4 5 | if($post->post_type == 'my_custom_post_type'){ return dirname(__FILE__) ."/my_custom_post_type-comments.php"; } |
Hope someone finds these snippets for how to override comments.php useful.
Disclaimer: All content on this site, is use at your own risk (Always backup before changing anything in your software/database/servers etc). Techs change, go out of date etc...
I/we accept no liability if anything you use on this site adversely affects you.
This is a nice article.