PHP的WordPress的代码回声的不是术语? [英] PHP Wordpress code echo's slug not term?
问题描述
有人知道我如何更改以下代码以用于自定义分类法(在Magic Fields 2插件中创建)来解决我的问题。我希望它回显所选值的名称
而不是 slug
格式。例如我想回显客户端1
和客户端2
而不是 client-1
和 client-2
一样。
Does anybody know how I can change the below code for my custom taxonomy (created in the Magic Fields 2 plugin) to solve my problem. I would like it to echo the name
of the selected value rather than slug
format. e.g. I'd like to echo Client 1
and Client 2
instead of client-1
and client-2
as it currently does.
我想显示带空格的多字名称并正确大写,例如 Joe Bloggs Associates
不是 joe-bloggs-associates
。
I'd like to display multiword names with spaces and correct capitalisation e.g. Joe Bloggs Associates
not joe-bloggs-associates
.
project_statistics_client
是在魔术字段中创建的字段的名称。自定义字段的类型为 Term
下拉列表,并填充自定义分类法中名为 Clients
的值。该字段位于名为 project_statistics
的字段组内,但是我不确定该组名是否会影响代码?
project_statistics_client
is the name of the field created in Magic Fields. The Type of the custom field is Term
dropdown and populates with the values from my custom Taxonomy called Clients
. This field is located inside of a field group that's named project_statistics
but I am not sure if the group name influences the code or not?
还请注意,以上所有内容均位于名为 Projects
的自定义帖子类型中。
Also please note that all the above is in a custom post type called Projects
.
I已检查插件帮助,但仍不确定:
I have checked plugin help but still not sure:
这是代码:
<?php $clients = get_field('project_statistics_client');
foreach($clients as $client){
echo '<div>' . $client . '</div>';
} ?>
推荐答案
global $post;
$taxonomy = 'your taxonomy';
$terms = get_the_terms($post->ID, $taxonomy);
if ( is_array($terms) ) {
echo(implode(',', wp_list_pluck($terms, 'name')));
}
Magic Fields 2只为基本的WordPress分类法功能提供了一个不错的GUI,因此可以使用所有WordPress分类法功能。特别是,get_the_terms()可用于获取帖子的类别。这将返回一个对象数组(一个帖子可能属于多个类别)。另外,您需要从对象中提取名称字段。
Magic Fields 2 only provides a nice GUI to the basic WordPress taxomony feature and therefore all the WordPress taxonomy functions can be used. In particular, get_the_terms() may be used to get the categories of a post. This returns an array of objects (a post may be in multiple categories). Also, you need to extract the 'name' field from the objects.
这篇关于PHP的WordPress的代码回声的不是术语?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!