Broken markup when using the_excerpt() in a widget?

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:

Read More

widget rendered

The title should be at the top. The reason is that, the excerpt isn’t where it is supposed to be:

enter image description here

Related posts

Leave a Reply

1 comment