从自定义发布类型创建分类

时间:2014-12-09 作者:user64544

我知道这个问题可能被问过很多次,但我尝试过谷歌搜索,但没有成功(至少没有使用插件)

我有一个“出版商”的自定义帖子类型,其中包含每个出版商的所有详细信息。当我写博客文章时,我希望有一个预先填充了所有发布者的分类法,最好只允许输入现有发布者。

然后,在每个发布者页面上,我想使用slug或ID(针对该发布者)进行WP查询,以查找标记有该发布者的所有帖子(有些帖子讨论多个发布者)。

我知道有一些插件可以让我这样做,甚至可以做更多的事情,我可以将相同的标签分配给发布者和帖子,但我希望避免出现两端都必须标记的情况。一旦一个出版商保存了一个slug和标题,我想能够用它来标记一篇文章。

我不是在寻找实现这一点的确切代码,而是寻找以尽可能最简单的方式实现这一点的布局/结构。

1 个回复
SO网友:Amit Mishra

用于为自定义帖子类型创建分类的非常简单的代码段

Register Custom Post Type

function Knowledge_base_init() {
$labels = array(
    \'name\'               => _x( \'Knowledge bases\', \'post type general name\', \'your-plugin-textdomain\' ),
    \'singular_name\'      => _x( \'Knowledge base\', \'post type singular name\', \'your-plugin-textdomain\' ),
    \'menu_name\'          => _x( \'Knowledge bases\', \'admin menu\', \'your-plugin-textdomain\' ),
    \'name_admin_bar\'     => _x( \'Knowledge base\', \'add new on admin bar\', \'your-plugin-textdomain\' ),
    \'add_new\'            => _x( \'Add New\', \'Knowledge base\', \'your-plugin-textdomain\' ),
    \'add_new_item\'       => __( \'Add New Knowledge base\', \'your-plugin-textdomain\' ),
    \'new_item\'           => __( \'New Knowledge base\', \'your-plugin-textdomain\' ),
    \'edit_item\'          => __( \'Edit Knowledge base\', \'your-plugin-textdomain\' ),
    \'view_item\'          => __( \'View Knowledge base\', \'your-plugin-textdomain\' ),
    \'all_items\'          => __( \'All Knowledge bases\', \'your-plugin-textdomain\' ),
    \'search_items\'       => __( \'Search Knowledge bases\', \'your-plugin-textdomain\' ),
    \'parent_item_colon\'  => __( \'Parent Knowledge bases:\', \'your-plugin-textdomain\' ),
    \'not_found\'          => __( \'No Knowledge bases found.\', \'your-plugin-textdomain\' ),
    \'not_found_in_trash\' => __( \'No Knowledge bases found in Trash.\', \'your-plugin-textdomain\' )
);

$args = array(
    \'labels\'             => $labels,
    \'public\'             => true,
    \'publicly_queryable\' => true,
    \'show_ui\'            => true,
    \'show_in_menu\'       => true,
    \'query_var\'          => true,
    \'rewrite\'            => array( \'slug\' => \'Knowledge_base\' ),
    \'capability_type\'    => \'post\',
    \'has_archive\'        => true,
    \'hierarchical\'       => false,
    \'menu_position\'      => null,
    \'menu_icon\' => admin_url() . \'images/media-button-video.gif\',
    \'supports\'           => array( \'title\', \'editor\', \'author\')
);

register_post_type( \'knowledge-base\', $args );
}


add_action( \'init\', \'create_knowledge_base_taxonomies\', 0 );
function create_knowledge_base_taxonomies() {
// Add Tips taxonomies
$labels = array(
    \'name\'              => _x( \'Tips & Tricks\', \'taxonomy general name\' ),
    \'singular_name\'     => _x( \'Tips & Tricks\', \'taxonomy singular name\' ),
    \'search_items\'      => __( \'Search Tips & Tricks\' ),
    \'all_items\'         => __( \'All Tips & Tricks\' ),
    \'parent_item\'       => __( \'Parent Tips & Tricks\' ),
    \'parent_item_colon\' => __( \'Parent Tips & Tricks:\' ),
    \'edit_item\'         => __( \'Edit Tips & Tricks\' ),
    \'update_item\'       => __( \'Update Tips & Tricks\' ),
    \'add_new_item\'      => __( \'Add New Tips & Tricks\' ),
    \'new_item_name\'     => __( \'New Tips & Tricks Name\' ),
    \'menu_name\'         => __( \'Tips & Tricks\' ),
);

$args = array(
    \'hierarchical\'      => true,
    \'labels\'            => $labels,
    \'show_ui\'           => true,
    \'show_admin_column\' => true,
    \'query_var\'         => true,
    \'rewrite\'           => array( \'slug\' => \'tips\' ),
);

register_taxonomy( \'tips\', array( \'knowledge-base\' ), $args );
}
帮助您创建分类法。

结束

相关推荐