Why are not displayed all the data of the post?

Bringing the comments block in the page template then everything is fine:
<div class="recall_block" style="background-image: url('<?php echo get_template_directory_uri() ?>/assets/img/bg_recall.jpg');">
 <div class="container">
 <div class="custom_title text-center">
 <h2>customer Testimonials</h2>
</div>
 <div class="row">
<?php

 // default settings
 $args = array(
 'numberposts' =--> 3,
 'post_type' => 'reviews',
 'suppress_filters' => true, // the suppression of the filters modify the SQL query
);

 $posts = get_posts( $args );

 foreach($posts as $post): setup_postdata($post)?>
 <div class="col">
 <div class="recall">
 <div class="picture">
 <img src="<?php the_post_thumbnail_url( 'full' );?>">
</div>
 <div class="name"><?php the_title(); ?></div>
 <p><?php echo get_the_content(); ?></p>
</div>
</div>
 <?php endforeach;
 wp_reset_postdata(); // reset
?>
</div>
 <div class="row">
 <div class="col text-center">
 <div class="watch_all"><a href="#/reviews/">view all</a></div>
</div>
</div>
</div>
 </div>

5a9181400ce7d259441578.png

And outputting the code of a function it is not displayed all the data:
if ( ! function_exists( 'mebel_reviews' ) ) {

mebel_reviews function(){
?>
 <div class="recall_block" style="background-image: url('<?php echo get_template_directory_uri() ?>/assets/img/bg_recall.jpg');">
 <div class="container">
 <div class="custom_title text-center">
 <h2>customer Testimonials</h2>
</div>
 <div class="row">
<?php

 // default settings
 $args = array(
 'numberposts' =--> 3,
 'post_type' => 'reviews',
 'suppress_filters' => true, // the suppression of the filters modify the SQL query
);

 $posts = get_posts( $args );

 foreach($posts as $post): setup_postdata($post)?>
 <div class="col">
 <div class="recall">
 <div class="picture">
 <img src="<?php the_post_thumbnail_url( 'full' );?>">
</div>
 <div class="name"><?php the_title(); ?></div>
 <p><?php echo get_the_content(); ?></p>
</div>
</div>
 <?php endforeach;
 wp_reset_postdata(); // reset
?>
</div>
 <div class="row">
 <div class="col text-center">
 <div class="watch_all"><a href="http://mebel.amtex.by/reviews/">view all</a></div>
</div>
</div>
</div>
</div>
<?php
}
}</property-->
br><img src="https://habrastorage.org/webt/5a/91/81/5a9181eb541d6797599932.png" alt="5a9181eb541d6797599932.png">
June 7th 19 at 15:39
1 answer
June 7th 19 at 15:41
Solution
Try at the beginning of the function, add global $post; (or before the foreach).
Weird, but worked. Do not tell me why? - cade.Beier commented on June 7th 19 at 15:44
this fragment setup_postdata($post) sets the post from the current loop iteration to the global variable $post. Working in the template you are in global scope, so this variable is available and everything works. Working within the function you are limited to the scope of this function, so the global variable $post is not available to you, and this code sets a local variable $post. And template tags only work with global, which has not changed. Using the global keyword, we "import" into the current scope (function), and the code may change. - Velva.Rogahn commented on June 7th 19 at 15:47

Find more questions by tags WordPress