0

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.

1

$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);
1
  • 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. Nov 11 '11 at 1:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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