0

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);
}
1
  • 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). May 22, 2012 at 14:21

1 Answer 1

2

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

Your Answer

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

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