문제
각 루프가 3 개의 루프를 가지고 있습니다.
도움이 크게 감사 할 것입니다
<?php
$i;
$args = array( 'numberposts' => 8);
$posts= get_posts( $args );
if ($posts) {
foreach ( $posts as $i => $post )
{
setup_postdata($post);
}
}
?>
<div class="item" id="<?php echo $i; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
<div class="col-md-6 nopadding view view-ninth" style="">
<?php the_post_thumbnail('large'); ?>
<div class="content">
<h2><?php the_title(); ;?></h2>
<p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . '...'; ?></p>
<a href="<?php the_permalink() ?>" class="info">Read More >></a>
</div>
</div>
<?php $a++; endforeach; ?>
</div><!-- end item -->
<div class="item" id="<?php echo $i; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
<div class="col-md-6 nopadding view view-ninth" style="">
<?php the_post_thumbnail('large'); ?>
<div class="content">
<h2><?php the_title(); ;?></h2>
<p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . '...'; ?></p>
<a href="<?php the_permalink() ?>" class="info">Read More >></a>
</div>
</div>
<?php $a++; endforeach; ?>
</div>
<div class="item" id="<?php echo $i+1; ?>" >
<?php
$a = 0;
foreach( $posts as $i => $post ) :
if ($a == 4) { break; }
?>
<div class="col-md-6 nopadding view view-ninth" style="">
<?php the_post_thumbnail('large'); ?>
<div class="content">
<h2><?php the_title(); ;?></h2>
<p><?php $excerpt = get_the_excerpt(); echo string_limit_words($excerpt,15) . '...'; ?></p>
<a href="<?php the_permalink() ?>" class="info">Read More >></a>
</div>
</div>
<?php $a++; endforeach; ?>
</div>
. 해결책 2
<?php $args = array( 'numberposts' => 8);
$posts= get_posts( $args );
$i=0;
foreach($posts as $key=>$value) {
if($i==0) { echo "<div class=\"item\" id=\"".$key."\" >"; } ?>
<div class="col-md-6 nopadding view view-ninth" style="">
<?php echo get_the_post_thumbnail($value->ID, 'large'); ?>
<div class="content">
<h2><?php echo $value->post_title; ?></h2>
<p><?php echo string_limit_words($value->post_excerpt,15) . '...'; ?></p>
<a href="<?php echo get_permalink($value->ID); ?>" class="info">Read More >></a>
</div>
</div>
<?php $i++; if($i==4) { $i=0; echo "</div><div class=\"item\" id=\"<?php echo $key; ?>\" >"; } } ?>
</div>
.
결국 내 수정 사항입니다.
다른 팁
WP_Query
를 사용하여 사용자 정의 루프를 만들고 offset
매개 변수를 사용하여 건너 뜁니다.이전 루프의 게시물.하나의 쿼리에서 3 개의 루프를 실행할 것입니다. 따라서 쿼리를 다시 실행하는 데 필요한 모든 작업은 rewind_posts()
를 사용하는 것입니다. 각 루프가 루프를 재설정하는 후
다음은 정확한 요구 사항을 제품군으로 수정할 수있는 예제 코드입니다.이것은 매우 기본적인 루프
입니다.<?php
$args = array( 'posts_per_page' => 4);
$my_query= new WP_Query( $args ); ?>
<ul>
<?php
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
$my_query->rewind_posts();
?>
<ul>
<?php
$my_query->query('posts_per_page=4&offset=4');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
$my_query->rewind_posts();
?>
<ul>
<?php
$my_query->query('posts_per_page=4&offset=8');
while ($my_query->have_posts()) : $my_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
endwhile;
?>
</ul>
<?php
wp_reset_postdata();
?>
. 제휴하지 않습니다 wordpress.stackexchange