毫无疑问,WordPress 是一个博客平台,可以在一个地方管理大量帖子、多个类别和标签。 关于标签、类别、帖子和所有内容的有趣事实可以用自定义帖子类型或自定义分类法进行管理和替换。
你有没有听说过 WordPress中的自定义分类? 如果不是,那么在本文中,我们将向您展示它是什么以及如何创建分类法。
分类
它是每个人都使用的一种,但其中许多人不知道它是什么以及如何使用它。 根据生物学的观点,Taxonomy 用于一起做群组帖子和自定义帖子。
分类法在WordPress中分为两种方法,标签和类别。 您根据类别创建帖子并添加标签,这意味着您将两个组放在一个地方或帖子中。
让我们考虑一个例子。 假设你有一个时尚类别,你必须在衣服、珠宝、鞋子等上写字。 因此,您可以将时尚类别划分为子类别,例如衣服,您可以只谈论衣服,等等。
此外,您可以在有关男装和女装的服装中创建更多子类别。 这意味着您有每个主题的子类别。 这称为自定义分类法。
这很简单,每个人都这样做,但不知道确切的名称。 现在,重点是如何在 WordPress 中创建自定义分类法。 为此,我们共享了两种方法。
一种方法 - 使用不想玩编码的插件。 第二种方法-您可以选择代码方法,或者您可以在不使用插件的情况下完成所有操作。
如果您想阅读更多内容,请继续阅读。
如何使用插件创建分类
如果您准备安装分类法,那么首先您需要安装一个名为 Simple Taxonomy 的插件。 为此,请按照给定的步骤操作:
1. 打开 WordPress 仪表板并转到设置。
2. 点击自定义分类并添加新的。
3. 现在,您的第一步是根据您的需要为分类命名。
注意 - 确保所有字母都是小写且没有字符。
1.下一步是分层,如果要创建分类广告Category,则必须选择true,您可以在其中添加一个幼崽类别。 如果您确实要添加标签,请选择 false。
2. 接下来,您需要选择帖子类型,所以在这里选择帖子而不是其他帖子。
3.下一步是Associate,选择none。 这将要求您添加自动条款。
假设您创建了一个分类并命名了它的主题。 现在,您的工作是告诉 WordPress 您需要使用哪种语言来翻译该主题。
因此,为此,请转到翻译措辞并选择翻译。 然后单击分类按钮并创建它。 完成后,它将位于进一步分类和标签的帖子下。 这也将出现在帖子编辑区域中。
这就是您使用插件时的全部内容,但是如果您想玩代码怎么办?
如何使用代码创建自定义分类?
如果您是这方面的专家,我们建议您采取此步骤。 在您的编码中添加以下代码。 因此,首先,打开 Functions.php 文件以创建分类。
对于分层,请使用以下代码:
//hook into the init action and call create_book_taxonomies when it firesadd_action( 'init', 'create_topics_hierarchical_taxonomy', 0 );
//create a custom taxonomy name it topics for your posts
function create_topics_hierarchical_taxonomy() {
// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI
$labels = array(
'name' = _x( 'Topics', 'taxonomy general name' ),
'singular_name' = _x( 'Topic', 'taxonomy singular name' ),
'search_items' = __( 'Search Topics' ),
'all_items' = __( 'All Topics' ),
'parent_item' = __( 'Parent Topic' ),
'parent_item_colon' = __( 'Parent Topic:' ),
'edit_item' = __( 'Edit Topic' ),
'update_item' = __( 'Update Topic' ),
'add_new_item' = __( 'Add New Topic' ),
'new_item_name' = __( 'New Topic Name' ),
'menu_name' = __( 'Topics' ),
);
// Now register the taxonomy
register_taxonomy('topics',array('post'), array(
'hierarchical' = true,
'labels' = $labels,
'show_ui' = true,
'show_admin_column' = true,
'query_var' = true,
'rewrite' = array( 'slug' = 'topic' ),
));
}
要创建像标签这样的非分层自定义分类法,请将此代码添加到主题的 functions.php 或特定于站点的插件中:
//hook into the init action and call create_topics_nonhierarchical_taxonomy when it firesadd_action( 'init', 'create_topics_nonhierarchical_taxonomy', 0 );
function create_topics_nonhierarchical_taxonomy() {
// Labels part for the GUI
$labels = array(
'name' = _x( 'Topics', 'taxonomy general name' ),
'singular_name' = _x( 'Topic', 'taxonomy singular name' ),
'search_items' = __( 'Search Topics' ),
'popular_items' = __( 'Popular Topics' ),
'all_items' = __( 'All Topics' ),
'parent_item' = null,
'parent_item_colon' = null,
'edit_item' = __( 'Edit Topic' ),
'update_item' = __( 'Update Topic' ),
'add_new_item' = __( 'Add New Topic' ),
'new_item_name' = __( 'New Topic Name' ),
'separate_items_with_commas' = __( 'Separate topics with commas' ),
'add_or_remove_items' = __( 'Add or remove topics' ),
'choose_from_most_used' = __( 'Choose from the most used topics' ),
'menu_name' = __( 'Topics' ),
);
// Now register the non-hierarchical taxonomy like tag
register_taxonomy('topics','post',array(
'hierarchical' = false,
'labels' = $labels,
'show_ui' = true,
'show_admin_column' = true,
'update_count_callback' = '_update_post_term_count',
'query_var' = true,
'rewrite' = array( 'slug' = 'topic' ),
));
}
如何显示分类
要显示分类,请使用以下代码:
the_terms ( $post-ID,’topics’,’Topics:’,’,’,);
您也可以将其添加到其他文件中,例如archive.php、index.php 以及您想要显示分类的任何其他位置。