rwmb_meta values unset after a shortcode
- This topic has 2 replies, 2 voices, and was last updated 9 years, 5 months ago by
Anh Tran.
-
AuthorPosts
-
December 3, 2015 at 5:13 PM #1845
JTLR
ParticipantOn all my page templates I have a checkbox which says whether to show a call to action before the footer (A strip with a button which links to the contact page). This is what's in the footer:
<?php if (rwmb_meta("truvision_footer_show_cta")): ?> <div class="strip strip--large-top-padding strip--large-bottom-padding bg--pattern"> <div class="container"> <div class="row"> <div class="col-xs-12 text--center"><a href="<?php echo get_permalink(rwmb_meta('truvision_footer_cta_link')); ?>" class="button button--secondary"><?php echo rwmb_meta('truvision_footer_cta_text'); ?></a> </div> </div> </div> </div> <?php endif; ?>
It works fine, apart from when a certain shortcode is present. If this shortcode is rendered on a page all the rwmb_meta() functions return values as if they hadn't been set. This is the shortcode:
function services() { $services = new WP_Query(array('post_type' => 'service-post', 'order' => 'ASC')); if ( $services->have_posts() ) : $content = '<div class="services js-services">'; $services_id = 0; while ( $services->have_posts() ) : $services->the_post(); if ($services_id %4 === 0 || $services_id === 0) { $content .= "<div class='row'>"; } $content .= '<div class="col-xs-12 col-sm-3"><div class="bolt-on"><img src="' . wp_get_attachment_image_src( get_post_thumbnail_id(), 'full')[0] . '" alt="Realistic stills gallery" class="bolt-on_icon"><h3 class="bolt-on_title js-bolt-on_title">' . get_the_title() . '</h3>' . get_the_content() . '</div></div>'; $services_id++; if ($services_id %4 === 0 && $services_id != 0) { $content .= "</div>"; } endwhile; $content .= '</div>'; endif; return $content; } add_shortcode('services', 'services');
Any idea what's happening??
December 3, 2015 at 8:44 PM #1848JTLR
ParticipantI don't know what the issue is, but I got around it by making global variables containing the rwmb_meta() info then echoing the content of the variable in the footer.
In each template:
$footer_cta_show = rwmb_meta("truvision_footer_show_cta");
In the footer
<?php global $footer_cta_show; if ($footer_cta_show): ?> ... <?php endif; ?>
December 3, 2015 at 11:40 PM #1850Anh Tran
KeymasterIn your shortcode, you use a custom query but you don't reset the query after outputting HTML. That makes the global
$post
object isn't reseted and contains wrong value of the current post. Thus, therwmb_meta
shortcode can't get the correct value.Best way to resolve this is adding
wp_reset_postdata()
after your query and try again. -
AuthorPosts
- The topic ‘rwmb_meta values unset after a shortcode’ is closed to new replies.