I’ve successfully created my post meta boxes, saved the data and I understand how to retrieve the data. Within the custom meta boxes I have a field for the page branding that will decide what color scheme per the product line we are featuring on that page.
I have a class per the color scheme that is triggered when the body has a class of the product line name appended to it. For example:
<body class="product-drinks">
OR
<body class="product-abcwidgets">
Depending on what product line is selected in the meta box for that post will determine what style sheet will be included.
For example if I chose “product-drinks” then the stylesheet included would be product-drinks.css.
Most of the meta box data I need to use within the loop but I also need to access the page branding mega field data outside of the loop. How would I grab this data if I need it outside of the loop?
I initially thought of placing some of the data in an array while in the loop as such and then referencing the $page_options array value in the body tag as such:
(outside of the loop in the header)
<body class="<?php echo $page_options['pageBranding'];?>
from within the loop”
$page_options = array(
'pageBranding' => get_post_meta($post_id, 'pageBranding', true),
'layout' => get_post_meta($post_id, 'pageLayout', true)
);
Am I doing this correctly or is there a better way of doing this? Or should I only reference the meta fields I need within the loop and then use global $wp_query; outside of the loop and get the post meta that way for the data I need for the body and stylesheets?
If you need the data outside the loop, I’d suggest using $wp_query or global $post.
Then you can call it just as normal