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.

Is it possible to get page's metabox value within shortcode that is executed on that page?

Scenario:

  • I have a sidebar metabox for every page.
  • I have some kind of custom gallery shortcode.

My gallery shortcode outputs 600x200 images (I'm using timthumb here).

BUT I want it to display 900x300 if there's no sidebar.

Normally I'd use:

$sidebar = get_post_meta($post->ID, 'metabox_sidebar', true);
   if($sidebar == "true") { do something }
   else { do something else }

But get_post_meta returns nothing within the shortcode body.

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

$post is outside the scope of your shortcode function, you have to globalize it first:

global $post;
$sidebar = get_post_meta($post->ID, 'metabox_sidebar', true);
share|improve this answer
    
Oh, my, God :) It's always like this, I'm working on complex things and everything works better than expected then something like this happens and I'm stuck. Thanks a lot, sorry for another super-lame question. –  Wordpressor Nov 11 '11 at 1:50
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.