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

时间:2015-09-10 07:41:49

标签: php html wordpress wordpress-theming

所以在我的search.php中它会给出结果。我希望每个结果都包含在div类中,以便我可以编辑它们并像长卡一样显示它们。但是目前我的代码只包装了第一个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(); ?>

1 个答案:

答案 0 :(得分:1)

您正在输出循环外的第一个<div>元素,以及循环内的close </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; ?>