我正在研究WordPress主题,但是我认为我的问题与PHP有关,这就是为什么我在这里发布。如果我错了,请纠正我。
我正在尝试将每3个循环项的集合包装在div标记中,但是有些不正确。下面是我到目前为止所做的代码,但总是以div损坏结束。
<?php if ( have_posts() ) : $i = 0; while ( have_posts() ) : the_post(); ?>
<?php if ( $i % 3 == 0) : ?>
<div class="articles-loop clearfix">
<?php endif; ?>
<article itemtype="https://schema.org/CreativeWork" <?php post_class(); ?>>
<header class="entry-header">
<h2 class="entry-title" itemprop="headline">
<a href="<?php the_permalink(); ?>" class="entry-title-link" rel="bookmark"><?php the_title(); ?></a>
</h2>
</header>
<div class="entry-content">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail('generic-grid-archive-featured'); ?>
</a>
<?php endif; ?>
<?php if ( has_excerpt() ): ?>
<p><?php $excerpt = excerpt(23); echo strip_tags($excerpt); ?></p>
<?php else : ?>
<p><?php $content = content(23); echo strip_tags($content); ?></p>
<?php endif; ?>
</div>
<!--
<footer class="entry-footer">
<div class="entry-meta clearfix">
<p class="read-more"><a href="<?php /*the_permalink(); */?>">Continue Reading</a></p>
<p class="author">Published by: <?php /*$author = get_the_author(); echo $author; */?></p>
</div>
</footer>
-->
</article>
<?php if ( $i % 3 != 0 ) : ?>
</div>
<?php endif; ?>
<?php $i++; endwhile; ?>
<?php if ( $i % 3 != 0 ) : ?>
</div>
<?php endif; ?>
<?php else: ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
我要实现的示例:
<div class="articles-loop clearfix">
Loop item 1
Loop item 2
Loop item 3
</div>
<div class="articles-loop clearfix">
Loop item 4
Loop item 5
Loop item 6
</div>
etc.
答案 0 :(得分:1)
尝试以下代码:
<?php
$loop_counter = $innerBreak = 1;
$wpb_all_query = new WP_Query(array('post_type'=>'page', 'post_status'=>'publish', 'posts_per_page'=>6));
if($wpb_all_query->have_posts()):
while($wpb_all_query->have_posts()) :
$wpb_all_query->the_post();
if($innerBreak == 1){
?>
<!-- when complete listing of 3 post open the new div -->
<div class="articles-loop clearfix">
<?php
}
?>
<article itemtype="https://schema.org/CreativeWork" <?php post_class(); ?>>
<header class="entry-header">
<h2 class="entry-title" itemprop="headline">
<a href="<?php the_permalink(); ?>" class="entry-title-link" rel="bookmark"><?php the_title(); ?></a>
</h2>
</header>
<div class="entry-content">
<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>
<?php /*if ( has_excerpt() ): ?>
<p><?php $excerpt = excerpt(23); echo strip_tags($excerpt); ?></p>
<?php else : ?>
<p><?php $content = content(23); echo strip_tags($content); ?></p>
<?php endif;*/ ?>
</div>
<!--
<footer class="entry-footer">
<div class="entry-meta clearfix">
<p class="read-more"><a href="<?php /*the_permalink(); */?>">Continue Reading</a></p>
<p class="author">Published by: <?php /*$author = get_the_author(); echo $author; */?></p>
</div>
</footer>
-->
</article>
<?php
//when complete listing of 3 post closed previously div.
if($loop_counter%3==0){ echo '</div>'; $innerBreak = 1;}else{$innerBreak = 0;}
$loop_counter++; endwhile;
else:
echo "<div>No Results Found</div>";
endif;
?>