The text returned by the_excerpt() does not respect my widget format. I couldn’t explain it better so I will just show you my code:
function widget($args, $options) {
extract($args);
$post_type = $options['post_type'];
$num_of_posts = $options['limit'];
// Create a new instance
$second_query = new WP_Query( array(
'post_type' => "$post_type",
'posts_per_page' => "$num_of_posts",
'post_status' => 'publish',
) );
// The Loop
if ($second_query->have_posts()) :
while( $second_query->have_posts() ) : $second_query->the_post();
$byh_content .= "<p>" . the_excerpt() . "</p>";
endwhile; else :
$byh_content = "<p>No " . $options['post_type'] . "s found.</p>";
endif;
wp_reset_postdata();
$title = "<h3>" . $options['title'] . "</h3>";
$content = $byh_content;
echo $before_widget.$title.$content.$after_widget;
}
The result isn’t what I expected. Here’s what it looks like when rendered:
The title should be at the top. The reason is that, the excerpt isn’t where it is supposed to be:
as you are working with strings, you need to use a function which returns the value, instead of echoing it;
in this line:
$byh_content .= "<p>" . the_excerpt() . "</p>";
use
get_the_excerpt()
http://codex.wordpress.org/Function_Reference/get_the_excerpt