PHP 仅将第一个搜索结果包装在 DIV 中

PHP only wrapping first search result in DIV

所以在我的 search.php 中它会给出结果。我希望将这些结果中的每一个都包装在 div class 中,以便我可以对它们进行全部编辑并像长卡片一样显示它们。但是目前我的代码只包装了第一个 DIV。无法真正弄清楚为什么会这样,因为代码会正确地循环每个结果并且应该将下一个结果重新包装在 DIV 中?那么这是我的代码和搜索结果页面。

我正在测试的搜索网站:This will take you straight to the search result page

<?php get_header(); ?>
<section id="content" role="main">
<?php if ( have_posts() ) : ?>
<header class="header">
<h1 class="entry-title"><?php printf( __( 'Search Results for: %s', 'blankslate' ), get_search_query() ); ?></h1>
</header>
<div class="item"><?php while ( have_posts() ) : the_post(); echo '<a href="' . get_permalink($post->ID) . '" >'; the_post_thumbnail('medium'); echo '</a>';?>

<?php get_template_part( 'entry' ); ?></div>
<?php endwhile; ?>
<?php get_template_part( 'nav', 'below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="header">
<h2 class="entry-title"><?php _e( 'Nothing Found', 'blankslate' ); ?></h2>
</header>
<section class="entry-content">
<p><?php _e( 'Sorry, nothing matched your search. Please try again.', 'blankslate' ); ?></p>
<?php get_search_form(); ?>
</section>
</article>
<?php endif; ?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

您正在输出循环外的第一个 <div> 元素,以及循环内的关闭 </div> 元素。将您的第一个移动到 while 循环中以使其正确。

<?php while ( have_posts() ) : the_post(); ?>
<div class="item">
    <?php echo '<a href="' . get_permalink($post->ID) . '" >'; the_post_thumbnail('medium'); echo '</a>';?>
    <?php get_template_part( 'entry' ); ?>
</div>
<?php endwhile; ?>