所有评论都列在下面"Comments" 没有创建单独"Comments" 自定义帖子类型的节/页。至少我不知道。
但是,您可以在评论列表中添加过滤器,以根据帖子类型过滤所有评论它就像帖子列表中的一个类别过滤器。
因此,您可以创建HTML select field
, 其中包含option for every custom post-type
. 这个value
每个选项的slug of the post-type
. 这个select
要素需求a name of "post_type"
使过滤工作正常。
/**
* Create custom comments filter for post types
**/
function my_comments_filter() {
// we are only getting the none-default post types here (no post, no page, no attachment)
// you can also change this, just take a look at the get_post_types() function
$args = array(
\'public\' => true,
\'_builtin\' => false
);
$post_types = get_post_types( $args, \'objects\' ); // we get the post types as objects
if ($post_types) { // only start if there are custom post types
// make sure the name of the select field is called "post_type"
echo \'<select name="post_type" id="filter-by-post-type">\';
// I also add an empty option to reset the filtering and show all comments of all post-types
echo \'<option value="">\'.__(\'All post types\', \'my-textdomain\').\'</option>\';
// for each post-type that is found, we will create a new <option>
foreach ($post_types as $post_type) {
$label = $post_type->label; // get the label of the post-type
$name = $post_type->name; // get the name(slug) of the post-type
// value of the optionsfield is the name(slug) of the post-type
echo \'<option value="\'.$name.\'">\'.$label.\'</option>\';
}
echo \'</select>\';
}
}
// we add our action to the \'restrict_manage_comments\' hook so that our new select field get listet at the comments page
add_action( \'restrict_manage_comments\', \'my_comments_filter\' );
如果将此代码添加到
functions.php
, 或者更好,
to a plugin
, 您将在评论后端的顶部看到一个新过滤器。在这里你可以
select a custom post-type
然后单击
Filter
, 筛选注释。如果选择“所有帖子类型”并再次单击“筛选”,则应显示所有评论。