Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm to dumb right now …

print_r($terms);

does this …

Array
(
    [7] => stdClass Object
        (
            [term_id] => 7
            [name] => Testwhatever
            [slug] => testwhatever
            [term_group] => 0
            [term_taxonomy_id] => 7
            [taxonomy] => event_type
            [description] => 
            [parent] => 0
            [count] => 2
            [object_id] => 8
        )

)

How can I print the slug? I thought print print($terms->slug) should do the job, but it says: "Trying to get property of non-object"

update:

function get_event_term($post) {
    $terms = get_the_terms( (int) $post->ID, 'event_type' );
    if ( !empty( $terms ) ) {
        print_r($terms);
        return $terms[7]->slug;
    }
}
share|improve this question

5 Answers 5

up vote 1 down vote accepted

Its an array of objects (even if it contains only a single entry at index "7"), not a single object

echo $terms[7]->slug;

With multiple "things

foreach ($terms as $term) echo $term->slug;
share|improve this answer
    
Thank you, I updated my question. The problem is that this "7" is a dynamic number specifically for each post in wordpress. I have no idea what number it is. It doesn't seem to be the post->ID –  matt Jul 18 '12 at 9:24
    
@matt Array $terms always has one element, or it could have more? –  Zagor23 Jul 18 '12 at 9:29
    
it could have more I guess. Normally it's set to one, but it is possible to add multiple terms to a post. –  matt Jul 18 '12 at 9:30
    
I have updated my answer based on that. –  Zagor23 Jul 18 '12 at 9:52

try

print_r($terms[7]->slug);

you object stdclass is in array[7] offset.

share|improve this answer

Or, to complicate things a little bit:

array_walk($terms, function($val,$key) use(&$terms){ 
    var_dump($val->slug);
});
share|improve this answer
    
thank you, this might be it. –  matt Jul 18 '12 at 9:30

Try this.

print ($terms[7]->slug);
share|improve this answer

print_r ($terms[7]->slug) looks logical to me, since it's an array of objects

UPDATE

Since you are unsure how many items get_event_term should return, you should return an Array.

function get_event_term($post) {
    $aReturn = array();
    $terms = get_the_terms( (int) $post->ID, 'event_type' );
    if ( !empty( $terms ) ) {
        print_r($terms);
        foreach($terms as $term){ // iterate through array of objects
            $aReturn[] = $term->slug; // get the property that you need
        }
    }
    return $aReturn;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.