0

I know how to change the title of the comment form by using comment_form_defaults, but I don't know how to make it different for a custom post type from a regular post. Also I would like to have the verbiage different in my comments.php file. Is it possible to have the comments.php file different for a custom post type, like you can do the header and footer?

2 Answers 2

0

You can use the comment_form_defaults filter to change your text based on custom post type. Just check the post type in your filter function.

For example:

add_filter( 'comment_form_defaults', 'my_comment_form_defaults' );
function my_comment_form_defaults( $defaults ) {

     if ( 'my_cool_custom_post_type' == get_post_type() ) {

          $defaults['title_reply'] == 'My Custom Post Type Comment Title';
     }
     return $defaults;
}

If you need to change various elements of the defaults, see where $defaults is defined just before the filter hook is applied in the source here: https://core.trac.wordpress.org/browser/tags/5.7.1/src/wp-includes/comment-template.php#L2429

Rather than load a different comments.php based on post type, you could potentially use some switch() or if() logic based on get_post_type() to display differently based on the requested post type.

2
  • This looks exactly what I was looking for. I really appreciate your answer.
    – Wayne G
    Commented May 2, 2021 at 12:48
  • Glad that was helpful. If this answers your question, would you please mark it as the accepted answer?
    – butlerblog
    Commented May 3, 2021 at 14:01
0

The answer given by butlerblog is correct. Just a small correction. (As I dont have the reputation to comment, I am posting it as my answer.)

The following line

$defaults['title_reply'] == 'My Custom Post Type Comment Title';

should be:

$defaults['title_reply'] = 'My Custom Post Type Comment Title';

1
  • This is really a comment, not an answer. With a bit more rep, you will be able to post comments. Commented Jun 22, 2022 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.