0

I have created a custom page type, and I would like users to be able to comment on it. At first blush this is similar to Comments not showing in custom post type - Wordpress, however I am setting the 'supports' value with 'comments':

 public function createEpisodeType() {
     $labels = array(
         'name'             => 'Episodes',
         'singular_name'   => 'Episode',
         'menu_name'       => 'Episodes',
         'name_admin_bar'     => 'Episode',
         'add_new'           => 'Add New',
         'add_new_item'     => 'Add New Episode',
         'new_item'         => 'New Episode',
         'edit_item'       => 'Edit Episode',
         'view_item'       => 'View Episode',
         'all_items'       => 'All Episodes',
         'search_items'     => 'Search Episodes',
         'parent_item_colon'  => 'Parent Episode',
         'not_found'       => 'No Episodes Found',
         'not_found_in_trash' => 'No Episodes Found in Trash',
     ); 

     $args = array(
         'labels'              => $labels,
         'public'              => true,
         'exclude_from_search' => false,
         'publicly_queryable'  => true,
         'show_ui'             => true,
         'show_in_nav_menus'   => true,
         'show_in_menu'        => true,
         'show_in_admin_bar'   => true,
         'menu_position'       => 5,
         'menu_icon'           => 'dashicons-admin-appearance',
         'capability_type'     => 'page',
         'hierarchical'        => true,
         'supports'            => array( 'page-attributes', 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments', 'revisions' ),
         'has_archive'         => true,
         'rewrite'             => array( 'slug' => 'episode', 'with_front' => false ),
         'query_var'           => true,
         'description'         => 'Page type that has a sequence',
    ); 

    register_post_type(self::post_type_slug, $args);
}

When I use Debug This to inspect current query, I see the post data, including

[comment_status] => open

I suspect the problem is with the theme customization. When I look at the comments.php file for the theme I am using, I see this right at the top:

$comments_display = get_theme_mod( 'comments_display' );
$post_type        = get_post_type();

if ( is_array( $comments_display ) ) {

    if ( ! in_array( $post_type, $comments_display ) ) {
        return;
    }
}

I directly inspected the options in the database, I see that 'comments_display' is an array of three values - 'post', 'page', and 'none'. My custom type is not in that list. That seems like the root of the problem to me.

I went to the Customize: Comment Display section for my theme, but the options did not include my new content type.

Is there a way to get my custom type to show up in that list? I'm assuming the theme calls some standard function to get a list of content types, and that I need to do something to get my type on that list.

1 Answer 1

1

I'm assuming the theme calls some standard function to get a list of content types, and that I need to do something to get my type on that list.

It's impossible to say without seeing the original code for creating options, but if I had to guess I'd say it's likely that they're manually entered. Any use of a standard function to list post types would probably already list yours.

So while it might not be possible to add your post type as an option, you can use the theme_mod_{$name} hook to programatically add your post type to the value of the theme mod so that the logic in comments.php is true for your post type.

The way this filter works is that you add a filter to theme_mod_comments_display and use the callback function to modify the value. Since the value appears to be an array of post types for which to show comments, we want to add episode (or whatever the actual slug is) to the array.

function wpse_299549_theme_mod( $value ) {
    // Make sure $value is an array.
    $value = (array) $value;

    // Add custom post type to array.
    $value[] = 'episode';

    // Return modified value.
    return $value;
}
add_filter( 'theme_mod_comments_display', 'wpse_299549_theme_mod' );

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.