Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working with a plugin called media tags...

The foreach looks as following (simplied as much as possible)

foreach($mediatags as $mediatag){
 $args = 'media_tags=animals&numberposts=10&return_type=li&size=thumbnail';
 $media_items = get_attachments_by_media_tags($args);
 echo $media_items;
};

running the exact same code outisde a foreach:

$args = 'media_tags=animals&numberposts=10&return_type=li&size=thumbnail';
 $media_items = get_attachments_by_media_tags($args);
 echo $media_items;

Works great, but when I run it inside the foreach I get the followin error:

Call to a member function get_attachments_by_media_tags() on a non-object in /home/nuagency/dev.nuagency.se/wp-content/plugins/media-tags/mediatags_template_functions.php on line 128

Heading over to line 128 in that file I see:

function get_attachments_by_media_tags($args='')
{
    global $mediatags;

    return $mediatags->get_attachments_by_media_tags($args);
}
share|improve this question
    
For future reference, the critical part of the error message is: Call to a member function... on a non-object. It really doesn't have anything to do with the function itself, but rather with how you're changing how you use the data types (as Eugene explains below). –  Chip Bennett May 22 '12 at 14:21
add comment

1 Answer

up vote 2 down vote accepted

You use $mediatags variable in global scope as array in your loop, and then try to use the same variable as object in your function.

You override your global $mediatags variable in your loop. Try to use following code:

foreach($new_name_for_mediatags_array as $mediatag){
 $args = 'media_tags=animals&numberposts=10&return_type=li&size=thumbnail';
 $media_items = get_attachments_by_media_tags($args);
 echo $media_items;
};
share|improve this answer
add comment

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.