HMTL original rendered:
<div class="col-md-4">
<div class="row">
<a href="http://localhost/PHP/wordpress/3-blog-post/">3 - Blog Post</a>
</div>
<div class="row">
<img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />
</div>
</div>
HTML after setup_postdata( $post );
<div class="row">
<div class="col-md-4">
<div class="row">
<a href="http://localhost/PHP/wordpress/3-blog-post/">
3 - Blog Post </a>
</div>
<div class="row">
<img width="150" height="150" src="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg" class="attachment-thumbnail size-thumbnail wp-post-image" alt="" srcset="http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-150x150.jpg 150w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-300x300.jpg 300w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal-768x768.jpg 768w, http://localhost/PHP/wordpress/wp-content/uploads/2017/04/imagem-principal.jpg 900w" sizes="(max-width: 150px) 100vw, 150px" />
Bem-vindo ao WordPress. Esse é o seu primeiro post. Edite-o ou exclua-o, e então comece a escrever!
</div>
</div>
Weird things:
The code:
<div class="container">
<div class="row">
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post)
{
?><div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']); ?>">
<?php echo $post['post_title']; ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div><?php
}
wp_reset_query();
?>
</div>
</div>
</br>
</br>
<div class="container">
<div class="row">
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<div class="col-md-4">
<div class="row">
<a href="<?php echo get_the_permalink($post['ID']) ?>">
<?php echo $post['post_title'] ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
</div>
<div class="row">
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
</div>
</div>
When I change the position of the code and remove title and thumbnail, the code works well:
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<?php
$my_excerpt = get_the_excerpt($post['ID']);
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
I call the recent posts in my wordpress / woocommerce page, but when I call the excerpt, it didn't appear, title and thumbnail are ok. I have worked on it all day long, but I couldn't figure out what happen. When I change "echo $my_excerpt" with a simple word, it works. The problem should be when I put the content in a array. I had followed the documentation as you can see: https://codex.wordpress.org/Function_Reference/get_the_excerpt
Examples get_the_excerpt() can be used to retrieve and store the value in a variable, without outputting it to the page.
<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt; // Outputs the processed value to the page
?>
My code:
<?php
$args = array( 'numberposts' => '3' );
$recent_posts = wp_get_recent_posts( $args );
foreach($recent_posts as $post) { ?>
<div class="col-md-4">
<div class="row">
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo $post['post_title'] ?>
</a>
</div>
<div class="row">
<?php echo get_the_post_thumbnail($post['ID'], 'thumbnail'); ?>
<?php
$my_excerpt = get_the_excerpt();
if ( '' != $my_excerpt ) {
// Some string manipulation performed
}
echo $my_excerpt // Outputs the processed value to the page
?>
</div>
</div>
<?php
}
wp_reset_query();
?>
</div>
</div>