【问题标题】:Get a Value in the Author Page from a Custom Taxonomy从自定义分类法获取作者页面中的值
【发布时间】:2022-01-27 09:11:09
【问题描述】:

我创建了一个名为国籍的自定义分类法,然后我添加它以让用户通过使用 ACF 的自定义帐户编辑表单选择他/她的国籍。

我正在尝试将 Tax 值输入 Author.php 页面,但它返回为 Array , 这是我正在使用的代码:

  <?php 
    $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));

    $terms = wp_get_post_terms($post->ID, 'nationality' ,   $curauth);
    if ($terms){
    $out = array(
                'name'=>'<span>Nationality: </span>' 
                );   
          foreach ($terms as $term) 
          {
            $out[] = '<a  "    ' . '  href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
            echo join( '  ', $out ); 
          }
  ?> 

我也试过下面的代码:

  <?php
  $nationality = get_field('nationality', $curauth);
  $nationality = get_field_object('nationality', $curauth);
  echo $nationality['value']; 
  ?>

仍然给我一个数组。

字段类型为 select 且 Return Value 设置为 Term Object 或 Term ID 那么错误是 WP_Term 类的对象无法转换为字符串

任何想法如何使它正确。

谢谢! 艾哈迈德

【问题讨论】:

  • 如果正确,请将我的答案标记为正确,请... =)

标签: wordpress advanced-custom-fields custom-taxonomy author


【解决方案1】:

您是否尝试从当前用户那里获取自定义分类?试试这样:

get_the_terms( get_the_ID(), 'nationality' )

如果你确定它总是只有一个(考虑到有些人有多个国籍),只需访问第一个元素:

get_the_terms( get_the_ID(), 'nationality' )[0]

【讨论】:

  • 不幸的是,它不起作用。
【解决方案2】:

我这里有正确的代码:

$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality',  $curauth);
 if( $terms ): ?>
 
    <?php foreach( $terms as $term ): ?>
   
      <?php echo  $term->name ; ?>
      <?php endforeach; ?>
    

<?php endif; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-04-06
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多