如何在标准帖子中显示邮寄类型和分类,而不是在单独的标签中?

时间:2013-01-18 作者:Blanka

我想在“Posts”标签中直接显示一些分类法,作为标准的post,而不是像exp的“Books”这样的单独标签,但我不知道如何显示。有没有办法做到这一点?代码如下:

类PostTypes{

var $types = array();
var $taxonomy = array();

function __construct($types = array(), $taxonomy = array()){
    $this->types = $types + $this->types;
    $this->taxonomy = $taxonomy + $this->taxonomy;
    $this->init();
}

function init(){
    add_action( \'init\', array(&$this, \'register_post_type\',) );
    add_action( \'init\', array(&$this, \'register_taxonomy\',) );
}


function register_post_type(){
    global $options;

    foreach ($this->types as $k => $v){
        $vowels = array(\'a\', \'e\', \'i\', \'o\', \'u\');
        $a = \'a\';
        $slug = $v[\'slug\'];
        $name = $v[\'name\'];

        $plural = $name.\'s\';
        $menu_name = $plural;
        if(isset($v[\'menu-name\']))
            $menu_name = $v[\'menu-name\'];

        $labels = array(
            \'name\'                  => __( $plural, \'post type general name\' ),  
            \'singular_name\'         => __( $name, \'post type singular name\' ),  
            \'add_new\'               => __( \'Add New\', strtolower( $name ) ),  
            \'add_new_item\'          => __( \'Add New \' . $name ),  
            \'edit_item\'             => __( \'Edit \' . $name ),  
            \'new_item\'              => __( \'New \' . $name ),  
            \'all_items\'             => __( \'All \' . $plural ),  
            \'view_item\'             => __( \'View \' . $name ),  
            \'search_items\'          => __( \'Search \' . $plural ),  
            \'not_found\'             => __( \'No \' . strtolower( $plural ) . \' found\'),  
            \'not_found_in_trash\'    => __( \'No \' . strtolower( $plural ) . \' found in Trash\'),  
            \'parent_item_colon\'     => \'\',  
            \'menu_name\'             => $menu_name

        );


        $supports = array(\'title\',\'editor\',\'thumbnail\', \'comments\');

        if(isset($v[\'supports\'])){
            $supports = $v[\'supports\'];
        }

        $args = array(
            \'labels\' => $labels,
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'query_var\' => false,
            \'rewrite\' => array(\'slug\' => $slug),
            \'capability_type\' => \'post\',
            \'has_archive\' => false,
            \'hierarchical\' => false,
            \'menu_position\' => 5,
            \'taxonomies\' => array(),
            \'supports\' => $supports
        );
        register_post_type($k, $args);


    }
}


function register_taxonomy(){

    foreach ($this->taxonomy as $k => $v){

        $taxonomy_slug =  $v[\'slug\'];

        $name = $v[\'name\'];
        $plural = $name.\'s\';
        $types = $v[\'post-types\'];


        $labels = array(
            \'name\' => __( $name, \'taxonomy general name\' ),
            \'singular_name\' => __( $name, \'taxonomy singular name\' ),
            \'search_items\'          => __( \'Search \' . $name ), 
            \'popular_items\' => __( \'Popular \'.$name ),
            \'all_items\' => __( \'All \'.$name ),
            \'parent_item\' => null,
            \'parent_item_colon\' => null,
            \'edit_item\' => __( \'Edit \'.$name ), 
            \'update_item\' => __( \'Update \'.$name ),
            \'add_new_item\' => __( \'Add New \'.$name ),
            \'new_item_name\' => __( \'New \'.$name.\' Name\' ),
            \'separate_items_with_commas\' => __( \'Separate \'.$name.\' with commas\' ),
            \'add_or_remove_items\' => __( \'Add or remove \'.$name ),
            \'choose_from_most_used\' => __( \'Choose from the most used \'.$name ),
            \'menu_name\' => __( $name ),
        );  

        if($v[\'type\'] == \'category\'){

            register_taxonomy($k, $types, array(
                \'hierarchical\' => true,
                \'labels\' => $labels,
                \'show_ui\' => true,
                \'query_var\' => true,
                \'rewrite\' => array( \'slug\' => $taxonomy_slug, \'with_front\' => false),
            ));

        }elseif($v[\'type\'] == \'tags\'){
            register_taxonomy($k, $types, array(
                \'hierarchical\' => false,
                \'labels\' => $labels,
                \'show_ui\' => true,
                \'update_count_callback\' => \'_update_post_term_count\',
                \'query_var\' => true,
                \'rewrite\' => array( \'slug\' => $taxonomy_slug, \'with_front\' => false),
            ));


        }   

    }


}
}

1 个回复
SO网友:Milo

当你register a taxonomy, 第二个参数是其可用的post类型:

register_taxonomy( $taxonomy, $object_type, $args );
如果希望分类法适用于自定义类型和标准帖子,可以传递类型数组:

$object_type = array( \'your_custom_type\', \'post\' );
或者,您可以使用register_taxonomy_for_object_type:

register_taxonomy_for_object_type( \'your_custom_taxonomy\', \'post\' );

结束

相关推荐

Page for custom taxonomy

我是WordPress的新手,我刚刚在一个名为arts的自定义帖子类型下创建了一个名为categories的新自定义分类法。有没有办法创建一个自定义页面来显示“类别”的自定义类型?这样,用户可以导航到艺术/类别,并查看类别分类法下的所有术语。

如何在标准帖子中显示邮寄类型和分类,而不是在单独的标签中? - 小码农CODE - 行之有效找到问题解决它

如何在标准帖子中显示邮寄类型和分类,而不是在单独的标签中?

时间:2013-01-18 作者:Blanka

我想在“Posts”标签中直接显示一些分类法,作为标准的post,而不是像exp的“Books”这样的单独标签,但我不知道如何显示。有没有办法做到这一点?代码如下:

类PostTypes{

var $types = array();
var $taxonomy = array();

function __construct($types = array(), $taxonomy = array()){
    $this->types = $types + $this->types;
    $this->taxonomy = $taxonomy + $this->taxonomy;
    $this->init();
}

function init(){
    add_action( \'init\', array(&$this, \'register_post_type\',) );
    add_action( \'init\', array(&$this, \'register_taxonomy\',) );
}


function register_post_type(){
    global $options;

    foreach ($this->types as $k => $v){
        $vowels = array(\'a\', \'e\', \'i\', \'o\', \'u\');
        $a = \'a\';
        $slug = $v[\'slug\'];
        $name = $v[\'name\'];

        $plural = $name.\'s\';
        $menu_name = $plural;
        if(isset($v[\'menu-name\']))
            $menu_name = $v[\'menu-name\'];

        $labels = array(
            \'name\'                  => __( $plural, \'post type general name\' ),  
            \'singular_name\'         => __( $name, \'post type singular name\' ),  
            \'add_new\'               => __( \'Add New\', strtolower( $name ) ),  
            \'add_new_item\'          => __( \'Add New \' . $name ),  
            \'edit_item\'             => __( \'Edit \' . $name ),  
            \'new_item\'              => __( \'New \' . $name ),  
            \'all_items\'             => __( \'All \' . $plural ),  
            \'view_item\'             => __( \'View \' . $name ),  
            \'search_items\'          => __( \'Search \' . $plural ),  
            \'not_found\'             => __( \'No \' . strtolower( $plural ) . \' found\'),  
            \'not_found_in_trash\'    => __( \'No \' . strtolower( $plural ) . \' found in Trash\'),  
            \'parent_item_colon\'     => \'\',  
            \'menu_name\'             => $menu_name

        );


        $supports = array(\'title\',\'editor\',\'thumbnail\', \'comments\');

        if(isset($v[\'supports\'])){
            $supports = $v[\'supports\'];
        }

        $args = array(
            \'labels\' => $labels,
            \'public\' => true,
            \'publicly_queryable\' => true,
            \'show_ui\' => true,
            \'show_in_menu\' => true,
            \'query_var\' => false,
            \'rewrite\' => array(\'slug\' => $slug),
            \'capability_type\' => \'post\',
            \'has_archive\' => false,
            \'hierarchical\' => false,
            \'menu_position\' => 5,
            \'taxonomies\' => array(),
            \'supports\' => $supports
        );
        register_post_type($k, $args);


    }
}


function register_taxonomy(){

    foreach ($this->taxonomy as $k => $v){

        $taxonomy_slug =  $v[\'slug\'];

        $name = $v[\'name\'];
        $plural = $name.\'s\';
        $types = $v[\'post-types\'];


        $labels = array(
            \'name\' => __( $name, \'taxonomy general name\' ),
            \'singular_name\' => __( $name, \'taxonomy singular name\' ),
            \'search_items\'          => __( \'Search \' . $name ), 
            \'popular_items\' => __( \'Popular \'.$name ),
            \'all_items\' => __( \'All \'.$name ),
            \'parent_item\' => null,
            \'parent_item_colon\' => null,
            \'edit_item\' => __( \'Edit \'.$name ), 
            \'update_item\' => __( \'Update \'.$name ),
            \'add_new_item\' => __( \'Add New \'.$name ),
            \'new_item_name\' => __( \'New \'.$name.\' Name\' ),
            \'separate_items_with_commas\' => __( \'Separate \'.$name.\' with commas\' ),
            \'add_or_remove_items\' => __( \'Add or remove \'.$name ),
            \'choose_from_most_used\' => __( \'Choose from the most used \'.$name ),
            \'menu_name\' => __( $name ),
        );  

        if($v[\'type\'] == \'category\'){

            register_taxonomy($k, $types, array(
                \'hierarchical\' => true,
                \'labels\' => $labels,
                \'show_ui\' => true,
                \'query_var\' => true,
                \'rewrite\' => array( \'slug\' => $taxonomy_slug, \'with_front\' => false),
            ));

        }elseif($v[\'type\'] == \'tags\'){
            register_taxonomy($k, $types, array(
                \'hierarchical\' => false,
                \'labels\' => $labels,
                \'show_ui\' => true,
                \'update_count_callback\' => \'_update_post_term_count\',
                \'query_var\' => true,
                \'rewrite\' => array( \'slug\' => $taxonomy_slug, \'with_front\' => false),
            ));


        }   

    }


}
}

1 个回复
SO网友:Milo

当你register a taxonomy, 第二个参数是其可用的post类型:

register_taxonomy( $taxonomy, $object_type, $args );
如果希望分类法适用于自定义类型和标准帖子,可以传递类型数组:

$object_type = array( \'your_custom_type\', \'post\' );
或者,您可以使用register_taxonomy_for_object_type:

register_taxonomy_for_object_type( \'your_custom_taxonomy\', \'post\' );

相关推荐

Custom Taxonomy Category link

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