0

I've got page with metabox called "something". It can have two values "value" and "value2".

I'm using a shortcode at this page and want it to display something's value. The problem is I don't know how to access global variable in a shortcode?

Example, I have this in page.php:

if($something == "value") {   
     global $one, $two; 
     $one = 120;
     $two = 240;
}

Now I want to access these variables in my shortocde (functions.php file), but

echo $one; echo $two;

Gives no output.

Any ideas?

1

Meta data is save in postmeta table and to get them you can use get_post_meta($post_id,'meta_key_name',true);

so in your shortcode function globilaize $post object and get you value like that

if($something == "value") {   
     global $post; 
     $one = get_post_meta($post->ID,'value_name'true);
     $two = get_post_meta($post->ID,'value2_name'true);
}
  • actually in my case I believe I can't do what I want using meta data. I have timthumb generated images and based on site layout I want to change their dimensions. And I'm not able to get given pages layout inside functions.php. – Wordpressor Apr 19 '11 at 17:55
1

Unless you're firing those echos from within a function through page.php, functions wont get anything from those globals because they are set up after functions.php runs.

Try this:

in functions.php:

function show_me_that_stuff() {
    global $one, $two;
    echo $one;
    echo $two;
}

then in page.php:

if($something == "value") {   
     global $one, $two; 
     $one = 120;
     $two = 240;
}
show_me_that_stuff();

Now it should output 120240.

0

Post specific values like you're demonstrated should be post_meta as demonstrated by Bainternet. No question.

However, if it isn't page specific and is truly global, you also have the wp_options table with a specific API for dealing with that.

  • Yeah my answer is more of a php explanation rather than a wordpress answer, my bad. – Drew Gourley Apr 18 '11 at 21:57

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.