我正在尝试创建一个插件,我需要一个自定义帖子和分类法。但它可以在管理端访问,但分类法在前端不起作用。
这是我注册自定义帖子和分类法的方式:
function post_type_questionnaire()
{
$labels = array(
\'name\' => _x(\'Questionnaire\', \'post type general name\'),
\'singular_name\' => _x(\'Questionnaire\', \'post type singular name\'),
\'add_new\' => _x(\'Add New Question\', \'questionnaire\'),
\'add_new_item\' => __(\'Add New Questionnaire\')
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => true,
\'_builtin\' => false, // It\'s a custom post type, not built in!
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(\'title\',
//\'editor\',
/*\'excerpt\',
\'thumbnail\',
\'trackbacks\',
\'custom-fields\',
\'comments\',
\'revisions\',
\'author\',
\'page-attributes\'*/
));
register_post_type(\'questionnaire\',$args);
}
add_action(\'init\', \'post_type_questionnaire\');
function create_questionnaire_taxanomies(){
register_taxonomy(\'qcategories\',\'questionnaire\', array(
\'hierarchical\'=>true,
\'label\'=>\'Questionnaire Categories\',
\'rewrite\' => array( \'slug\' => \'questionnaire\' )
));
}
add_action(\'init\', \'create_questionnaire_taxanomies\',0);
我正在使用一个短代码在前端显示它。
使用下面的代码显示所有帖子
$args = array(
\'post_type\' => \'questionnaire\',
\'posts_per_page\' => -1,
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) :query->the_post(); ?>
但当我尝试指定一个分类术语时,它不起作用,代码如下:
$args = array(
\'post_type\' => \'questionnaire\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'qcategories\',
\'field\' => \'slug\',
\'terms\' => $atts["name"]
)
)
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
当我试图在模板页面中显示帖子数量时,它什么也没有显示:
$term = get_term( 3, \'qcategories\' );
echo $term->count;
这不会输出任何东西,所以我相信分类法没有注册,有人能帮我在自定义插件中注册分类法吗。提前感谢!
SO网友:stillatmylinux
您可能需要重新保存永久链接。只需按永久链接设置上的保存按钮:/wp admin/options permalink。php
否则,您的代码只需清除几个拼写错误即可工作:
function post_type_questionnaire()
{
$labels = array(
\'name\' => _x(\'Questionnaire\', \'post type general name\'),
\'singular_name\' => _x(\'Questionnaire\', \'post type singular name\'),
\'add_new\' => _x(\'Add New Question\', \'questionnaire\'),
\'add_new_item\' => __(\'Add New Questionnaire\')
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => false,
\'_builtin\' => false, // It\'s a custom post type, not built in!
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array(
\'title\',
//\'editor\',
/*\'excerpt\',
\'thumbnail\',
\'trackbacks\',
\'custom-fields\',
\'comments\',
\'revisions\',
\'author\',
\'page-attributes\'*/
));
register_post_type(\'questionnaire\',$args);
}
add_action(\'init\', \'post_type_questionnaire\');
function create_questionnaire_taxanomies(){
register_taxonomy(\'qcategories\',\'questionnaire\', array(
\'hierarchical\'=>true,
\'label\'=>\'Questionnaire Categories\',
\'rewrite\' => array( \'slug\' => \'questionnaire\' )
));
}
add_action(\'init\', \'create_questionnaire_taxanomies\',0);
此模板代码有一些拼写错误,但可能会引发错误:
$atts["name"] = \'stackexchange\';
$args = array(
\'post_type\' => \'questionnaire\',
\'posts_per_page\' => -1,
\'tax_query\' => array(
array(
\'taxonomy\' => \'qcategories\',
\'field\' => \'slug\',
\'terms\' => $atts["name"]
)
)
);
$query = new WP_Query($args);
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php } ?>