【问题标题】:PHP Wordpress code echo's slug not term?PHP Wordpress 代码回显的 slug 不是术语?
【发布时间】:2013-02-26 19:29:49
【问题描述】:

有谁知道我可以如何更改我的自定义分类(在 Magic Fields 2 插件中创建)的以下代码来解决我的问题。我希望它回显所选值的 name 而不是 slug 格式。例如我想回显Client 1Client 2,而不是像现在这样的client-1client-2

我想显示带有空格和正确大小写的多字名称,例如Joe Bloggs Associates 不是 joe-bloggs-associates

project_statistics_client 是在 Magic Fields 中创建的字段的名称。自定义字段的类型是 Term 下拉列表,并使用我的自定义分类法中的值填充,称为 Clients。此字段位于名为 project_statistics 的字段组内,但我不确定组名是否会影响代码?

另外请注意,以上所有内容都在一个名为 Projects 的自定义帖子类型中。

我已经检查了plugin 的帮助,但仍然不确定:

代码如下:

<?php $clients = get_field('project_statistics_client');
    foreach($clients as $client){
        echo '<div>' . $client . '</div>';
    } ?>

【问题讨论】:

  • 我想知道添加 $client->name 是否会有所帮助。蛞蝓消失了,没有出现错误,但什么也没显示?使用 echo get_label('project_statistics_client');成功返回字段的标签,所以我想知道返回名称而不是 slug 是否是不同的 get_?到处都找不到答案,一直在网上搜索。
  • 使用 _get('field_name');似乎适用于我尝试过的所有其他内容,看起来只有“自定义分类”值在您用它们填充术语字段时显示为 slug。在 Magic Fields 插件设置下,我看不到将分类法设置为“名称”而不是“slug”的方法,此选项在自定义分类法列表本身或用于的术语下拉菜单中都不存在显示这些值。?我确定有一个相对简单的答案,我就是找不到。

标签: php wordpress taxonomy custom-fields


【解决方案1】:
    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() 可用于获取帖子的类别。这将返回一个对象数组(一篇文章可能属于多个类别)。此外,您需要从对象中提取“名称”字段。

【讨论】:

  • 嘿洋红色,感谢您的反馈。可以使用所有标准的 Wordpress 分类功能,这很好。即使使用自定义帖子类型也是如此吗?我尝试了您在上面用 PHP 标记提供的代码,因为它正在回显代码,它没有错误,但它没有显示术语数组。该页面与其他元素一起显示,但数组应该在哪里没有什么?我已将其放入循环中的模板页面中。那是对的吗?我将 'your taxonomy' 更改为 'project_statistics_client' 和 'name' 我保留为 'name'。
  • WordPress 分类功能适用于自定义帖子类型。我已经测试了代码,所以我知道它可以工作。您是否使用了正确的分类蛞蝓?在 Magic Fields 页面上查找分类 slug 名称以获取分类。当然'name'应该是'name'。
  • 分类标签显示在魔术字段“帖子类型”页面的“类型”列下。
  • 好的,分类实际上是“客户端”,我之前也尝试过,但这仍然没有显示。我不知道我做错了什么,我知道你的代码一定是正确的,因为你已经测试过了。我不明白为什么它什么也不显示,我知道分类法存在,因为在我正在查看的页面上它仍然与 slug 版本相呼应。所以我知道他们在场?这很奇怪。我的模板页面名为 projectLayoutA.php,我刚刚在 php 标签内的循环下添加了代码。我也在使用自定义帖子类型,这就是为什么要询问它们。谢谢
  • 我怀疑分类法不存在,而您正在从其他地方呼应某些东西?在类别窗格中的自定义帖子页面编辑器上,是否实际检查了类别的复选框?尝试使用自定义帖子页面编辑器上的类别窗格添加新类别。
【解决方案2】:

好的,过了一会儿我想起我从论坛帖子中粘贴了一些代码,这使我可以使用分类/类别选项填充术语下拉字段。我现在认为这就是问题所在,为什么它输出的是 slug 而不是名称?我还决定使用类别而不是分类法,因为我想根据所选值过滤帖子循环,并且从我读到的内容来看,使用类别更容易实现。我附在我添加到functions.php的代码下方,因为a)其他人可能想将它与Magic Fields一起使用,但也b)希望有人可能知道我需要更改什么才能输出名称而不是slug?

/**
* Custom Taxonomy Dropdown
*/



// initialisation
global $mf_domain;

// class with static properties encapsulating functions for the field type

class term_field extends mf_custom_fields {

public $allow_multiple = TRUE;
public $has_properties = TRUE;

public function _update_description(){
global $mf_domain;
$this->description = __("This field allows to do relations with taxonomie terms",$mf_domain);
}

public function _options(){
global $mf_domain;

// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}

$data = array(
 'option' => array(
    'term' => array(
      'type' => 'select',
      'id' => 'term',
      'label' => __('related taxonomy: ',$mf_domain),
      'name' => 'mf_field[option][term]',
      'default' => '',
      'options' => $select,
      'add_empty' => false,
      'description' => '',
      'value' => '',
      'div_class' => '',
      'class' => ''
    ),
  )
);
return $data;
}

public function display_field( $field, $group_index = 1, $field_index = 1 ) {
global $mf_domain;

// If is not required this field be added a None value
$notype = "";
if( !$field['required_field'] ) {
  $notype = ( !empty($field['options']['notype']) ) ? $field['options']['notype'] : __( "-- None --" , $mf_domain );
}

$output = '';

// Get the taxonomie as dropdownoption
$select = array();
$tax = get_taxonomies();
foreach($tax as $k => $v){
$select[] = $v;
}

$option_from_term_array = $field['options']['term'];
$options = get_terms($select[$option_from_term_array], array('hide_empty' => false));
$output = '<div class="mf-dropdown-box">';
$value = $field['input_value'];

$output .= sprintf('<select class="dropdown_mf" id="%s" name="%s" >',$field['input_id'],$field['input_name']);

if( $notype != "" ) {
  $output .= "<option value=''>$notype</option>";
}

foreach($options as $option) {

  $check = ($option->slug == $value) ? 'selected="selected"' : '';
  $output .= sprintf('<option value="%s" %s >%s</option>',
    esc_attr($option->slug),
    $check,
    esc_attr($option->name)
  );
}
$output .= '</select>';
$output .= '</div>';

return $output;
}
}

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 2012-05-20
    • 2012-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2015-10-26
    相关资源
    最近更新 更多