欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

wordpress 自定义_如何创建自己的自定义WordPress分类法

程序员文章站 2022-04-17 16:57:26
...

wordpress 自定义

This tutorial will help outline all that you need to know when it comes to WordPress taxonomies.

本教程将帮助概述有关WordPress分类法的所有必要知识。

We will begin by discussing what taxonomies are, how they can be interacted with, and why they are important.

我们将首先讨论什么是分类法,如何与它们相互作用,以及为什么它们很重要。

Later in the tutorial, we will go through the step-by-step process of creating your own custom taxonomies.

在本教程的后面,我们将逐步地创建您自己的自定义分类法。

By the end, you should have a strong grasp of what taxonomies are and how you can leverage your own custom taxonomies to suit your website needs.

最后,您应该对什么是分类法以及如何利用自己的自定义分类法来满足您的网站需求有深刻的了解。

什么是WordPress分类法? (What Are WordPress Taxonomies?)

Taxonomies, in their simplest sense, are a way to group together related items using one or more sets of words.

从最简单的意义上讲,分类法是一种使用一组或多组单词将相关项目组合在一起的方法。

WordPress uses taxonomies to provide a simple mechanism to group together your posts and pages. Out of the box, WordPress comes with two main types of taxonomies:

WordPress使用分类法提供了一种简单的机制来将您的帖子和页面组合在一起。 WordPress开箱即用,具有两种主要的分类法:

  • Categories

    分类目录
  • Tags

    标签

These two taxonomies provide you an easy way to group your posts together using names / groupings that make sense to you, and your readers. For example, you could create a new category called ‘Featured’ which you can then assign specific posts to.

这两种分类法为您提供了一种简单的方法,可以使用对您和您的读者有意义的名称/分组将您的帖子分组在一起。 例如,您可以创建一个名为“精选”的新类别,然后可以将特定帖子分配给该类别。

When you create a category or tag, you are creating a ‘Term’ within a ‘Taxonomy’. This is useful to know: categories and tags are just WordPress’ default implementation. You can create your own ‘Taxonomies’ with ‘Terms’ that suit your needs.

创建类别或标签时,即在“分类法”中创建“术语”。 这很有用:类别和标签只是WordPress的默认实现。 您可以使用适合您需要的“条款”来创建自己的“分类法”。

类别和标签管理区域 (Categories and Tags Admin Areas)

WordPress automatically creates administration areas for you to easily add your tags and categories. These administration areas are located inside the ‘Posts’ menu as sub-menus.

WordPress自动为您创建管理区域,以轻松添加标签和类别。 这些管理区域作为子菜单位于“职位”菜单内。

wordpress 自定义_如何创建自己的自定义WordPress分类法

Both the categories and tags administration screens will look similar, giving you an easy way to add new terms to your website.

类别和标签管理屏幕都将看起来相似,这为您提供了一种向网站添加新术语的简便方法。

wordpress 自定义_如何创建自己的自定义WordPress分类法

为帖子分配类别和标签 (Assigning Categories and Tags to Your Posts)

The easiest way to attach your categories and tags to your post is via the WordPress editor.

将类别和标签附加到帖子的最简单方法是通过WordPress编辑器。

When you are editing your post / page you will notice a metabox that contains your categories and tags. These boxes often appear underneath your ‘publish’ metabox. These boxes are what you will need to use to assign your terms to the currently edited post.

在编辑帖子/页面时,您会注意到一个包含类别和标签的元框。 这些框通常出现在“发布”元框的下方。 这些框是您需要用来将术语分配给当前编辑的帖子的框。

wordpress 自定义_如何创建自己的自定义WordPress分类法

Both the tag and category metaboxes will let you assign multiple terms to your post. You will be able to add terms you have created previously via their administration menus, or you can create and assign them on the fly (these new terms will then be selectable for other posts in the future).

标签和类别元框都可以让您为帖子分配多个术语。 您将能够通过其管理菜单添加之前创建的术语,或者可以动态创建和分配它们(以后,这些新术语将可供其他帖子选择)。

WordPress如何处理其分类法? (What Does WordPress Do with Its Taxonomies?)

WordPress will do a series of things with its in-built taxonomies, including:

WordPress将使用其内置的分类法执行一系列操作,包括:

  • Create a single term listing page: WordPress will create a new page for this term. The URL of the term itself will be the name of the taxonomy followed by the name of the term. For example, if you had a ‘category’ named ‘featured’, the URL would be /category/featured. The purpose of this page is essentially to act as a listing page.

    创建一个术语列表页面: WordPress将为此术语创建一个新页面。 术语本身的URL将是分类名称,然后是术语名称。 例如,如果您有一个名为“功能”的“类别”,则该URL为/ category / featured。 该页面的目的实质上是充当列表页面。

  • Create a link to the single taxonomy listing page on your individual posts: If you attach the in-built terms to your post, when you view your post on the front end, WordPress will display a clickable name of the term, generally directly under the title of the post. Clicking on the term will take you to the term listing page.

    在各个帖子上创建指向单个分类列表页面的链接:如果将内置术语附加到您的帖子中,则当您在前端查看帖子时,WordPress将显示该单词的可点击名称,通常直接在帖子标题。 单击该术语将带您到术语列表页面。

  • List your terms inside widgets: WordPress comes prebuilt with several ‘widgets’ that allow you to easily add content to your site’s widget areas (like sidebars and footers). Both your ‘categories’ and ‘tag cloud’ widgets will pull in your terms and display them as clickable links.

    在窗口小部件中列出您的术语: WordPress预先构建了几个“窗口小部件”,使您可以轻松地将内容添加到站点的窗口小部件区域(如边栏和页脚)。 您的“类别”和“标签云”小部件都将加入您的术语并将其显示为可点击的链接。

  • Add your terms to the navigation menu: All of your tags and categories are added to the navigation administration menu where you define and build your main menu. This allows you to easily create a link directly to your most commonly used term.

    将术语添加到导航菜单:将所有标签和类别添加到导航管理菜单,您可以在其中定义和构建主菜单。 这使您可以轻松地直接创建指向最常用术语的链接。

Depending on your theme, there may be be other areas that take advantage of your taxonomies.

根据您的主题,可能还有其他领域可以利用您的分类法。

创建自己的自定义分类法 (Creating Your Own Custom Taxonomies)

While the inbuilt categories and tags may suit your organisational needs, more often that not, you will need more ways in which to group together your posts, especially if you are using custom post types.

尽管内置的类别和标签可能适合您的组织需求,但通常情况并非如此,但您将需要更多的方式将您的帖子分组在一起,尤其是在使用自定义帖子类型时。

如何注册自己的自定义分类法 (How to Register Your Own Custom Taxonomy)

To create your own custom taxonomies you will need to define your taxonomy using the register_taxonomy function. This function takes in three values as follows register_taxonomy($taxonomy, $object_type, $args). A brief summary of these values are detailed below:

要创建自己的自定义分类法,您将需要使用register_taxonomy函数定义分类法。 该函数采用三个值,如下所示: register_taxonomy($taxonomy, $object_type, $args) 。 这些值的简要摘要如下:

  1. $taxonomy – Name of the new taxonomy you are creating. Where WordPress calls their taxonomy ‘categories’ or ‘tags’, you may want to give yours another name, such as ‘members’. This name has to be under 32 characters in length and may only use letters and the underscore character.

    $taxonomy –正在创建的新分类$taxonomy名称。 在WordPress称其分类法为“类别”或“标签”的地方,您可能想给自己起另一个名字,例如“ members”。 该名称的长度必须少于32个字符,并且只能使用字母和下划线字符。

  2. $object_type – Name of the post type to which you want to attach this taxonomy. WordPress’ post post type has both categories and tags attached to it. You may want to attach your new taxonomy to an existing post type, or your own custom post type that you have previously created. You have two options in this case:

    $object_type –要将此分类法附加到的帖子类型的名称。 WordPress的post类型具有附加的类别和标签。 您可能希望将新的分类法附加到现有的帖子类型,或者您以前创建的自定义帖子类型。 在这种情况下,您有两种选择:

    • A single string representing the name of the post type such as $object_type = 'post'

      表示帖子类型名称的单个字符串,例如$object_type = 'post'

    • An array of strings for the names of the post types such as $object_type = array('post','page')

      帖子类型名称的字符串数组,例如$object_type = array('post','page')

  3. $args – These are your arguments used to set the various options for your new taxonomy. There are multiple options that you can set. Several of these are mandatory, but most are optional (WordPress will handle any missing arguments):

    $args –这些是用于设置新分类法各种选项的参数。 您可以设置多个选项。 其中一些是强制性的,但大多数是可选的(WordPress将处理所有缺少的参数):

    • label – The plural name for your taxonomy, for example ‘members’, if you were creating a membership taxonomy.

      label –分类的复数名称,例如“成员”(如果您正在创建成员分类)。

    • labels – An array of your names and values that will be used for the taxonomy. These are used in administration areas for management. This array specifies all of the labels used for your taxonomy. If you leave this empty, WordPress will use your label value and set these. In addition, you can skip specific non-needed values and they will be defaulted.

      labels –将用于分类的名称和值的数组。 这些在管理区域中用于管理。 该数组指定用于分类法的所有标签。 如果您将此保留为空,WordPress将使用您的label值并进行设置。 此外,您可以跳过特定的不需要的值,它们将被默认设置。

      • name – Plural name of the taxonomy.

        name -分类法的复数名称。

      • singular_name – Singular name used for one term within the taxonomy.

        singular_name –在分类法中用于一个术语的singular_name数名称。

      • menu_name – Text to be displayed on the WordPress administration back-end (along the left hand side administration menu).

        menu_name –要在WordPress管理后端上显示的文本(以及左侧管理菜单)。

      • all_items – Viewing all terms from the taxonomy.

        all_items –从分类法中查看所有术语。

      • view_item – Viewing a single term from the taxonomy.

        view_item –从分类法中查看单个术语。

      • update_item – Updating a single taxonomy.

        update_item –更新单个分类法。

      • add_new_item – Add new term text.

        add_new_item –添加新的术语文本。

      • parent_item – Used with hierarchal taxonomies, generally set to Parent $taxonomy_name.

        parent_item –与层次分类法一起使用,通常设置为Parent $taxonomy_name

      • parent_item_colon – Same as above, but also adding a colon to the end.

        parent_item_colon –与上面相同,但在末尾添加了一个冒号。

      • search_items – Search text used when looking through your taxonomy.

        search_items –搜索分类法时使用的搜索文本。

      • popular_items – Popular term name, used in the back-end administration section for non hierarchical terms. Can easily be set to Popular $taxonomy_name.

        popular_items –流行术语名称,在后端管理部分中用于非分层术语。 可以轻松设置为Popular $taxonomy_name

      • separate_items_with_commas – This text is displayed for non-hierarchical taxonomies, this is the text shown on the taxonomy meta box for individual posts (this is shown directly under the ‘add’ button).

        separate_items_with_commas –对于非分级分类法,将显示此文本,这是在分类元数据框中显示的各个帖子的文本(直接在“添加”按钮下方显示)。

      • add_or_remove_items – This text is displayed for non-hierarchical taxonomies. This text is only shown with JavaScript disabled inside the taxonomy meta box for individual pages.

        add_or_remove_items –显示add_or_remove_items本用于非分层分类法。 仅在单个页面的分类法元框中禁用JavaScript的情况下显示此文本。

      • choose_from_most_used – This text is displayed for non-hierarchical taxonomies, at the bottom of the taxonomy meta box and when selected it will show a listing of the most used terms.

        choose_from_most_used –在非分类分类法中, choose_from_most_used本显示在分类法元框的底部,选中后将显示最常用术语的列表。

      • not_found – This text is displayed for non-hierarchical taxonomies, inside the taxonomy meta box only once you have clicked on the ‘Chose From Most used’ highlighted text. Once clicked, WordPress will look for the most used terms. If there are none, this text will be displayed.

        not_found –仅当您单击“从最常用的单词中选择”突出显示的文本时,才会在分类学元框中显示此非分层分类法的文本。 单击后,WordPress将查找最常用的术语。 如果没有,将显示此文本。

    • public – Determines if your taxonomy will be shown and be able to be queried against.

      public –确定是否将显示您的分类法并可以对其进行查询。

    • show_ui – Determines if WordPress will display an administration area for your taxonomy. If this is not set, you won’t have an area to manage your terms.

      show_ui –确定WordPress是否显示您的分类法的管理区域。 如果未设置,则将没有区域来管理您的条款。

    • show_in_nav_menus – Determines if the terms from this taxonomy will be selectable in your navigation menu.

      show_in_nav_menus –确定是否可以在导航菜单中选择此分类法中的术语。

    • show_tagcloud – Determines if WordPress will include your taxonomy’s terms inside the tag cloud widget.

      show_tagcloud –确定WordPress是否将您的分类学术语包含在标签云小部件中。

    • meta_box_cb – Lets you specify a function used to output the design of the taxonomies meta box inside your single posts. If not selected WordPress will use its default.

      meta_box_cb –可让您指定一个函数,用于在单个帖子中输出分类法元框的设计。 如果未选择,则WordPress将使用其默认值。

    • show_admin_column – This determines if your taxonomy’s terms will appear inside a new column for your post listings. Setting this to ‘true’ will display a new column for your specified post type that will display all of its attached terms.

      show_admin_column –这确定您的分类法术语是否将出现在帖子列表的新列中。 将其设置为“ true”将为您指定的帖子类型显示一个新列,该列将显示其所有附加条款。

    • hierarchical – Determines if your taxonomy can levels such as parents / children (like categories) or are all flat in level (like tags).

      hierarchical –确定您的分类法是可以级别(例如父/子)(例如类别)还是全部级别(例如标签)。

    • update_count_callback – Name of a function to call when there has been an update to the attached post type. When this taxonomy’s post type changes, this function will be called.

      update_count_callback –附加帖子类型的更新时要调用的函数的名称。 当此分类法的帖子类型更改时,将调用此函数。

    • query_var – Determines the name used for querying the post type. By default this is set to the name of the taxonomy itself. If set to a string, that string will be used. This is best left to its default value.

      query_var –确定用于查询帖子类型的名称。 默认情况下,此名称设置为分类法本身的名称。 如果设置为字符串,则将使用该字符串。 最好将其保留为默认值。

    • rewrite – This can be set in multiple ways. Setting this to ‘false’ will disable permalinks. If this has not been set to false, you can specify multiple element such as the ‘slug’. This is best left to its default true value.

      rewrite –可以通过多种方式进行设置。 将此设置为“ false”将禁用永久链接。 如果尚未将其设置为false,则可以指定多个元素,例如'slug'。 最好将其保留为默认真实值。

    • capabilities – Determines the capabilities (permissions) needed to interact with the taxonomy such as deleting, adding, assigning. This is best left to its default value.

      capabilities –确定与分类法交互(例如删除,添加,分配)所需的功能(权限)。 最好将其保留为默认值。

    • sort – Specifies that when assigning terms to a post, it should remember the order.

      sort –指定在为帖子分配术语时,它应该记住顺序。

    • _builtin – Determines if this term is a ‘built in’ taxonomy or a custom taxonomy. This should not be touched when creating your taxonomy

      _builtin –确定此术语是“内置”分类法还是自定义分类法。 创建分类法时不应触及

Remember to check the check the WordPress codex on custom taxonomies, as it outlines what values are mandatory and which are optional (along with what values are valid).

切记检查自定义分类法上的WordPress代码库 ,因为它概述了哪些值是必需的,哪些是可选的(以及哪些值有效)。

创建您的第一个自定义分类法 (Creating Your First Custom Taxonomy)

As you can see, there are several options that you can set to tweak your taxonomy for your own requirements.

如您所见,您可以设置几个选项来根据自己的需求调整分类法。

While you can individually set all of your taxonomies options, the majority of the time it is best to let WordPress fall back to its defaults settings.

虽然您可以单独设置所有分类法选项,但大多数情况下,最好还是让WordPress退回其默认设置。

For this tutorial, we are going to create a new taxonomy called Members which we will attach to our already existing Post post type.

在本教程中,我们将创建一个称为Members的新分类法,该分类法将附加到我们现有的Post post类型。

//create a function that will attach our new 'member' taxonomy to the 'post' post type
function add_member_taxonomy_to_post(){

    //set the name of the taxonomy
    $taxonomy = 'member';
    //set the post types for the taxonomy
    $object_type = 'post';
    
    //populate our array of names for our taxonomy
    $labels = array(
        'name'               => 'Members',
        'singular_name'      => 'Member',
        'search_items'       => 'Search Members',
        'all_items'          => 'All Members',
        'parent_item'        => 'Parent Member',
        'parent_item_colon'  => 'Parent Member:',
        'update_item'        => 'Update Member',
        'edit_item'          => 'Edit Member',
        'add_new_item'       => 'Add New Member', 
        'new_item_name'      => 'New Member Name',
        'menu_name'          => 'Member'
    );
    
    //define arguments to be used 
    $args = array(
        'labels'            => $labels,
        'hierarchical'      => true,
        'show_ui'           => true,
        'how_in_nav_menus'  => true,
        'public'            => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'member')
    );
    
    //call the register_taxonomy function
    register_taxonomy($taxonomy, $object_type, $args); 
}
add_action('init','add_member_taxonomy_to_post');

Executing the above code will create a new taxonomy with the name Members and attach it to the Post post type. When you view the back-end of your website you will now see a new sub-menu inside the Post menu. Clicking on this new Members sub-menu will take you to the taxonomy administration screen.

执行上述代码将创建一个名为Members的新分类法,并将其附加到Post post类型。 现在,当您查看网站的后端时,您将在“ Post菜单中看到一个新的子菜单。 单击此新的“ Members子菜单,将带您到分类管理屏幕。

wordpress 自定义_如何创建自己的自定义WordPress分类法

Your taxonomy administration screen will look like either the category or tag screens depending on what you set your hierarchical argument to. Our new member interface looks as follows:

分类管理屏幕将类似于category屏幕或tag屏幕,具体取决于您将hierarchical参数设置为什么。 我们的新member界面如下所示:

wordpress 自定义_如何创建自己的自定义WordPress分类法

创建新条款 (Creating Your New Terms)

With your taxonomy registered you can now create your new terms (in our case members) to your hearts content.

在注册了分类法之后,您现在可以为您的内容创建新的术语(在我们的情况下为成员)。

We have created three new top level terms. Our terms are called Featured Members,Standard Members and Seasonal Members.

我们创建了三个新的*术语。 我们的术语称为“ Featured Members ,“ Standard Members和“ Seasonal Members

Our featured and standard members are single level terms while our seasonal members are multi levelled, with two terms as its children called Winter Members and Summer Members.

我们的特色会员和标准会员是单级会员,而我们的季节性会员是多层会员,两个子Winter Members称为“ Winter Members和“ Summer Members

This is easy to visualise when you consider categories. Categories can have many sub-categories or even none at all.

当您考虑类别时,这很容易可视化。 类别可以有许多子类别,甚至根本没有。

wordpress 自定义_如何创建自己的自定义WordPress分类法

在您的帖子中添加您的条款 (Adding Your Terms to Your Post)

You can add as many terms as you like to your posts. You assign these terms by editing the post itself and using the taxonomy meta box to attach the terms.

您可以在帖子中添加任意数量的字词。 您可以通过编辑帖子本身并使用分类法元框来附加这些术语来分配这些术语。

wordpress 自定义_如何创建自己的自定义WordPress分类法

与分类法/术语进行交互 (Interacting with Your Taxonomy / Terms)

Once you have created and assigned your terms to your posts, you will need to modify your theme to take advantage of your new taxonomy.

创建帖子并将其分配给帖子后,您将需要修改主题以利用新的分类法。

WordPress will display its category and term related links when you view your individual posts. However, because you have registered your own taxonomy, you will need to customise your template to ensure that your terms are displayed.

当您查看各个帖子时,WordPress将显示其类别和与术语相关的链接。 但是,由于您已经注册了自己的分类法,因此需要自定义模板以确保显示您的条款。

更改主题文件以显示新的分类法 (Changing Your Theme Files to Show Your New Taxonomy)

As you have added an additional taxonomy, you will need to slightly modify your theme template files to ensure that your terms are shown. This involves editing a few theme files and interacting with some WordPress functions.

添加了其他分类法后,您将需要稍微修改主题模板文件,以确保显示您的条款。 这涉及编辑一些主题文件并与某些WordPress功能进行交互。

了解您的单个模板文件 (Understanding Your Single Template File)

If you are working on a child theme (and you should be), you can open up your single.php template file. While we won’t be editing this file, it’s important to know that this file handles the output of all single posts. Since we have attached our member taxonomy to posts, we will start here.

如果您正在处理儿童主题(应该这样做),则可以打开single.php模板文件。 虽然我们不会编辑此文件,但重要的是要知道此文件可以处理所有单个posts的输出。 由于我们已将member分类标准附加到posts ,因此我们将从这里开始。

Your single.php should look something like the following. I am using Twenty Thirteen as my parent theme, but overall most single.php files have the same core functionality.

您的single.php应该类似于以下内容。 我将二十三个十三作为我的父主题,但是总体而言,大多数single.php文件具有相同的核心功能。

<?php
get_header(); ?>
	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">
			<?php /* The loop */ ?>
			<?php while ( have_posts() ) : the_post(); ?>
				<?php get_template_part( 'content', get_post_format() ); ?>
				<?php twentythirteen_post_nav(); ?>
				<?php comments_template(); ?>
			<?php endwhile; ?>
		</div><!-- #content -->
	</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>

The main part you need to focus on is the get_template_part('content',get_post_format()). This function is what will actually display all of your single post content and it is here that we will need to perform our changes.

您需要关注的主要部分是get_template_part('content',get_post_format()) 。 此功能实际上将显示您的所有单个帖子内容,并且在这里我们需要执行更改。

了解您的内容模板文件 (Understanding Your Content Template File)

content.php is the file responsible for outputting your posts content. In actuality, it is the main file used to output all post type content, when there is no more specific template to load, it will load content.php.

content.php是负责输出您的帖子内容的文件。 实际上,它是用于输出所有帖子类型内容的主文件,当没有更多特定模板可加载时,它将加载content.php。

The first part of get_template_part() will take the name of the file you are trying to load. The second parameter (which is optional) will then look for a specific version of that file. As an example get_template_part('content','video') will look for a file called ‘content-video.php’. When called by itself, such as get_template_part('content’), it will look for a file named ‘content.php’

get_template_part()的第一部分将使用您尝试加载的文件的名称。 然后,第二个参数(可选)将查找该文件的特定版本。 例如, get_template_part('content','video')将寻找一个名为'content-video.php'的文件。 get_template_part('content')调用时,例如get_template_part('content') ,它将查找名为'content.php'的文件。

What we want to do is edit our content template part. However, this presents some difficulty because meta information such as taxonomies are handled differently in different themes.

我们要做的是编辑内容模板部分。 但是,由于诸如分类法之类的元信息在不同主题中的处理方式不同,因此存在一些困难。

  • Twenty Thirteen: In Twenty Thirteen, meta information is displayed using the twentythirteen_entry_meta() function. This function loops through your post’s categories / tags and is called from within content.php (and all of your other ‘content’ files such as ‘content-video.php’).

    二十十三岁:在二十十三岁中,使用二十十三十三twentythirteen_entry_meta()函数显示元信息。 此功能遍历您帖子的类别/标签,并在content.php(以及所有其他“内容”文件,例如“ content-video.php”)中调用。

  • Twenty Fourteen: In Twenty Fourteen, meta information is displayed inside of content.php directly (in addition, each of the other ‘content’ files such as ‘content-video.php’ also manually process this).

    第二十四章:在第二十四章中,元信息直接显示在content.php内部(此外,其他每个“内容”文件(例如“ content-video.php”)也手动处理此信息)。

创建一个函数以输出新的分类术语 (Creating a Function to Output Your New Taxonomy Terms)

Our solution will be to create a quick function that has the sole job of displaying our new ‘member’ terms.

我们的解决方案将是创建一个快速功能,其唯一工作就是显示新的“会员”条款。

Open up your functions.php and enter in the following code:

打开您的functions.php,然后输入以下代码:

function display_member_taxonomy_terms($post_id){
    
    //get all terms assigned to this post
    $member_terms = get_the_terms($post_id,'member'); 
    //if we have member terms assigned to this post
    if($member_terms){
        echo '<div class="member-terms-meta">';
        echo '<span class="term-title"> Member Terms: </span>';
        //loop through each term 
        foreach($member_terms as $term){
            //collect term information and display it
            $term_name = $term->name;
            $term_link = get_term_link($term,'member'); 
            echo '<a href="' . $term_link . '">'; 
                echo '<span class="term">' . $term_name . '</span>';
            echo '</a>';
        }
        echo '</div>';
    }
}

在内容文件中调用新功能 (Calling Your New Function Inside Your Content File)

Once you have added this function to your functions.php file, open up your content.php and find where your ‘entry-meta’ is being outputted. Call your new function inside this ‘entry-meta’ div. I have called our function above the standard Twenty Thirteen meta function as detailed below:

在将此功能添加到functions.php文件后,打开content.php并找到输出“ entry-meta”的位置。 在此“ entry-meta” div中调用新函数。 我已经在标准二十三十三元函数之上调用了我们的函数,如下所示:

<div class="entry-meta">
	<?php display_member_taxonomy_terms($post->ID); ?>
	<?php twentythirteen_entry_meta(); ?>
	<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '' ); ?>
</div><!-- .entry-meta -->

When you open up your single posts, you will see that the member terms are now displayed above the standard category and tag terms (so long as you have assigned member terms).

当您打开单个帖子时,您会看到member条款现在显示在标准类别和标记条款的上方(只要您已分配了成员条款)。

wordpress 自定义_如何创建自己的自定义WordPress分类法

了解我们所做的 (Understanding What We Just Did)

To get your new terms to display, what we did was first call a function named display_member_taxonomy_terms($post->ID);from within your content.php file. This function takes in the current post ID and attempts to show all terms belonging to our new taxonomy that have been assigned to this post.

为了显示您的新条款,我们首先调用了一个名为display_member_taxonomy_terms($post->ID);的函数display_member_taxonomy_terms($post->ID); 从您的content.php文件中。 该函数将获取当前帖子ID,并尝试显示属于我们新分类法的所有已分配给该帖子的术语。

We first called $member_terms = get_the_terms($post_id,'member'); to collect our terms for this post. The get_the_terms($post_id,$taxonomy_name) function takes in an ID of a post, along with a taxonomy name and gives us all attached terms for that taxonomy. In our situation, we passed it our current post id and also the ‘member’ taxonomy.

我们首先调用$member_terms = get_the_terms($post_id,'member'); 收集我们对此职位的条款。 get_the_terms($post_id,$taxonomy_name)函数获取帖子的ID和分类名称,并为我们提供该分类的所有附加术语。 在我们的情况下,我们将当前的职位ID以及“成员”分类法传递给了它。

Next, we check to see if we have any results using a simple if statement if($member_terms). If we have do have terms to display, we loop through each of our terms using our for loop foreach($member_terms as $term). This loop lets us iterate through each term and access its information.

接下来,我们使用简单的if语句if($member_terms)来检查是否有任何结果。 如果确实有要显示的术语,则使用for循环foreach($member_terms as $term)每个foreach($member_terms as $term) 。 此循环使我们可以遍历每个术语并访问其信息。

Finally, we accessed the name of the term (to display it to the user) and also fetched the URL of the term using the get_term_link($term_object,$taxonomy_name) function. This function takes in a term object and the name of the taxonomy, and gets the direct link to the term itself.

最后,我们访问了术语的名称(以显示给用户),并使用get_term_link($term_object,$taxonomy_name)函数获取了该术语的URL。 该函数接受术语对象和分类法的名称,并获得与术语本身的直接链接。

包装东西 (Wrapping Things Up)

As you can see, creating custom taxonomies can provide you with a wide range of new ways to organise your websites content such as your posts.

如您所见,创建自定义分类法可以为您提供各种组织网站内容(例如帖子)的新方法。

By creating a new taxonomy and associated terms and assigning them to a post, you can create a layer of organisation which you can leverage to further categorise content for your website.

通过创建新的分类法和相关术语并将其分配给帖子,您可以创建一个组织层,您可以利用该组织进一步对网站内容进行分类。

翻译自: https://www.sitepoint.com/custom-wordpress-taxonomies/

wordpress 自定义