rwmb_meta values unset after a shortcode

Support General rwmb_meta values unset after a shortcode

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #1845
    JTLRJTLR
    Participant

    On 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??

    #1848
    JTLRJTLR
    Participant

    I 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; ?>
    #1850
    Anh TranAnh Tran
    Keymaster

    In 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, the rwmb_meta shortcode can't get the correct value.

    Best way to resolve this is adding wp_reset_postdata() after your query and try again.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The topic ‘rwmb_meta values unset after a shortcode’ is closed to new replies.