2

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;
    }
}
0

6 Answers 6

3

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;
Sign up to request clarification or add additional context in comments.

3 Comments

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 Array $terms always has one element, or it could have more?
it could have more I guess. Normally it's set to one, but it is possible to add multiple terms to a post.
2

try

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

you object stdclass is in array[7] offset.

Comments

2

Or, to complicate things a little bit:

array_walk($terms, function($val,$key) use(&$terms){ 
    var_dump($val->slug);
});

Comments

0

Try this.

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

Comments

0

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

Comments

0

Function using the wp_parse_args system to manage its single $args argument, which could be given whatever values you wanted. In this case $args stores detailed display overrides, a pattern found in many WordPress functions.

$args = wp_parse_args( $args, $term->name );
echo $arg[157]['term_id']; //output 157
echo $arg[157]['name'];  //output Entertainment

works fine for me more detail

http://codex.wordpress.org/Function_Reference/wp_parse_args

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.