0

The code below is a bootstrap slider turned into a custom post type.This custom post type is working by itself but when i try to put it in shortcode it doesn't work.Specifically get_post_meta isn't working because i can see the attachment file working properly

<section id="reviews" class="reviews page">
            <div class="container wow fadeInUp">
                <div class="row">
                    <h1 class="sr-only"><?php echo $testimonialsArr['title']; ?></h1>
                    <div id="review-slider" class="carousel slide carousel-fade" data-ride="carousel" data-interval="false">
                        <div class="col-xs-12">

                            <?php 

                                $args=array(

                                    'post_type' => 'testimonials',
                                    'order'    => 'ASC'

                                );

                                $testimonialQuery = new WP_Query($args);

                            ?>
                            <?php if ( $testimonialQuery->have_posts() ) :?>
                            <ol class="carousel-indicators">
                            <?php  while( $testimonialQuery->have_posts() ) : $testimonialQuery->the_post(); 

                                $testimonialName = get_post_meta( $post->ID, '_published_testimonial_name_key',true);
                                var_dump($testimonialName);


                            ?>

                                <li data-target="#review-slider" data-slide-to="<?php echo $testimonialQuery->current_post ?>" class="<?php if( $testimonialQuery->current_post == 0 ):?>active<?php endif;?> wow fadeInLeft" data-wow-delay="0.3s"><?php echo $testimonialName;?></li>

                            <?php endwhile; ?>

                            </ol>
                            <?php wp_reset_postdata(); ?>
                        <?php endif; ?>
                        </div>
                        <!-- Wrapper for slides -->
                        <div class="col-xs-12">
                            <div class="row">
                                <div class="carousel-inner" role="listbox">
                                    <?php if ( $testimonialQuery->have_posts() ) : while( $testimonialQuery->have_posts() ) : $testimonialQuery->the_post();

                                        $testimonialText = get_post_meta( $post->ID, '_published_testimonial_text_key',true);
                                        $testimonialAvatar = wp_get_attachment_url( get_post_thumbnail_id() );
                                        $testimonialLink = get_post_meta( $post->ID, '_published_testimonial_url_key',true);

                                    ?>

                                    <div class="item <?php if( $testimonialQuery->current_post == 0 ):?>active<?php endif;?> clearfix fadeInUp delay-2">
                                        <div class="col-xs-12">
                                            <blockquote>

                                                <?php echo $testimonialText; ?>

                                            </blockquote>
                                        </div>


                                        <div class="col-xs-12 blockquote-cite">
                                            <div class="row no-gutter">
                                                <div class="col-xs-2 col-xs-offset-4 col-sm-2 col-sm-offset-5 col-md-1 col-md-offset-6">
                                                    <span class="cite-img"><img src="<?php echo $testimonialAvatar;?>" class="img-responsive" alt="published-img"></span>
                                                </div>
                                                <div class="col-xs-6 col-sm-5 col-md-5">
                                                    <div class="cite-right">
                                                        <span class="cite-name">Adam Butler</span>
                                                        <?php if($testimonialLink):?>
                                                            <span class="cite-url"><a href="<?php echo $testimonialLink; ?>"><?php echo $testimonialLink; ?></a></span>
                                                        <?php endif; ?>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>

                                    </div><!-- end item -->
                                        <?php endwhile;?>
                                        <?php wp_reset_postdata(); ?>
                                    <?php endif; ?>
                                    </div>
                                    <?php wp_reset_postdata(); ?>
                                </div>
                             </div>
                        </div>
                    </div>
                </div><!-- End: .row -->
            </div><!-- End: .container-->
        </section>
2

I believe the issue is that in the shortcode you don't actually have access to the global $post object.

  1. You could switch out your WP_Query with a get_posts() that returns an array.
  2. You could use get_the_ID();.
  3. You could call global $post at the top of your shortcode function.

The problem is that global $post is technically out of scope when used in this function. The Loop and Custom WP_Queries will work fine in templates because wp() puts $post in the global namespace for the templates to use whenever template is loaded all behind the scenes. As soon as you add any generic function, shortcode callback function, or add_shortcode() passing in an anonymous function as a 2nd parameter, the global variables will be out of scope until you bring them into scope by calling global $post.

To add onto this. The reason the get_*() functions work ( such as get_the_title() or get_the_ID() ) is because they all call get_post() which uses the global $post and bubbles the value upward.

To read more about this you can check out PHP Variable Scope or Variable Scope and WordPress by David Hayes

  • thank you it solved the problem.Can you direct me to a resource where i can learn more about this? – user16072 May 5 '17 at 19:06
  • @user16072 I've updated this answer to provide further clarification. – Howdy_McGee May 5 '17 at 19:47

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.