如何在自定义插件中设置分类标准?

时间:2017-02-17 作者:shellysaju

我正在尝试创建一个插件,我需要一个自定义帖子和分类法。但它可以在管理端访问,但分类法在前端不起作用。

这是我注册自定义帖子和分类法的方式:

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;
这不会输出任何东西,所以我相信分类法没有注册,有人能帮我在自定义插件中注册分类法吗。提前感谢!

1 个回复
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 } ?>

相关推荐

Custom Taxonomy Category link

我似乎在这里转圈转圈,我认为缺乏词汇或不知道我在寻找什么是造成我问题的原因。我有一个自定义的帖子类型projects 并有一个分类法来反映这一点当尝试在菜单中添加此帖子类型的类别链接时,它会在站点上链接为域。com/类别/自定义\\u类别理想情况下,我希望URL为:域。com/projects/custom\\u cat谁能给我指一下正确的方向吗。非常感谢!Edit:我想我展开了一个问题,创造了另一个问题,我取代了: register_taxonomy( \'category\', \'project\'