skip to Main Content

I have created a Custom Taxonomy called nationality, then I added it to let the user chose his/her nationality through a Custom Account Edit Form using ACF.

I am trying to get the Tax value into the Author.php page but it returns as Array ,
This is my code which I am using:

  <?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 ); 
          }
  ?> 

I have also tried the following code:

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

still giving me an Array.

The Field type is select and Return Value is set to Term Object either Term ID
the error then is
Object of class WP_Term could not be converted to string

any Ideas how to make this Correct.

Thank you!
Ahmad

2

Answers


  1. Chosen as BEST ANSWER

    I have the right Code here :

    $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; ?>
    

  2. are you trying to get the custom taxonomy from the current user? try like this:

    get_the_terms( get_the_ID(), 'nationality' )
    

    and if you are sure it will always get only one (take into account some people have more than one nationality) just access the first element:

    get_the_terms( get_the_ID(), 'nationality' )[0]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search