0

I have a variable $terms that contains:

 Array ( 
   [230] => stdClass Object ( 
      [term_id] => 230
      [name] => Executive Committee 
      [slug] => executive_committee
      [term_group] => 0 
      [term_taxonomy_id] => 241 
      [taxonomy] => team_member_filter
      [description] =>
      [parent] => 0 
      [count] => 1 
      [object_id] => 1561 
      [filter] => raw 
   )   
)

Each post contains this array, obviously the '230' key is different for each post. Inside of the default wordpress loop, i can print_r($terms) and it returns this array for every post. I need to echo the 'slug' value for each post. I can spit out the slug value by writing $terms[230]->slug, but of course this only returns the first instance. How do I return each posts 'slug' value in the loop?

Here's my loop:

<?php $args = array('post_type' => 'team-member','posts_per_page'=>-1,'order'=>'DESC','orderby'=>'date'); ?>
<?php query_posts($args); ?>
<?php $terms = get_the_terms(get_the_ID(), 'team_member_filter'); ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

        <!-- Get the taxonomy -->
        <?php print_r($terms[230]->slug); ?>

   <?php endwhile; ?>

How do I replace the '230' so that I get the 'slug' value for each post.

JordanBarber
  • 2,041
  • 5
  • 34
  • 62
  • Possible duplicate of [PHP - Extracting a property from an array of objects](http://stackoverflow.com/questions/1118994/php-extracting-a-property-from-an-array-of-objects) – dmgig Mar 11 '16 at 19:46
  • Can you add mode detail, perhaps the loop code you have – Mathieu de Lorimier Mar 11 '16 at 19:46
  • @MathieudeLorimier Thanks, edits made above. – JordanBarber Mar 11 '16 at 19:50
  • When you call , i am guessing that get_the_ID is returning probably 0 or null, therefore you are getting the terms of all your posts. You could move that line inside your while loop and you would end up with an array containing the terms for the post beeing iterated on. – Mathieu de Lorimier Mar 11 '16 at 20:03
  • But I need to get the array for all the posts in this query. I just need to be able to print that 'slugs' value for each post – JordanBarber Mar 11 '16 at 20:11

1 Answers1

1

You're using get_the_terms outside of the loop therefore it's returning the terms for the first post. It doesn't change as you loop through the posts.

Move $terms = ... inside the loop.

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php $terms = get_the_terms( get_the_ID(), 'team_member_filter' ); ?>

Next up $terms is an array. I would suggest looping through it.

<?php if ( $terms && ! is_wp_error( $terms ) ) { 
    foreach ( $terms as $term ) {
        echo $term->slug; // don't forget to format.
    }
} ?>

If you're sure there's only one you could always do the following instead:

<?php echo current( $terms )->slug; ?>

If you do take this route it's probably worth keeping the if statement just in case something went wrong.

Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58