PHP的新手,并寻找重复,但我可能正在寻找错误的术语
我有这个查询在运行查询时加载不同的帖子模板,所以我可以为博客主页加载更有趣的页面布局
<?php
$query_args = array(
'post_type'=> 'post', 'feature', 'fashion', 'film',
'posts_per_page' => 100,
'paged' => $paged,
'cat' => -13
);
$query = new WP_query ($query_args);
if ( $query->have_posts() ) {
$count = (0);
}
?>
<section class="blog-posts grid">
<?php while ( $query->have_posts() ) : $query->the_post();
if ( get_post_type()) {
switch($count) {
case 0: include( locate_template( 'includes/homepage-1of2.php' )); break;
case 1: include( locate_template( 'includes/homepage-1of2.php' )); break;
case 2: include( locate_template( 'includes/homepage-1of1.php' )); break;
case 3: include( locate_template( 'includes/homepage-1of3.php' )); break;
case 4: include( locate_template( 'includes/homepage-1of3.php' )); break;
case 5: include( locate_template( 'includes/homepage-1of3.php' )); break;
default: include( locate_template( 'includes/homepage-1of1.php' ));
}
$count++;
}
endwhile; ?>
<div class="load-more">
<button>Load More</button>
</div>
</section>
是否可以在案例中添加第n个孩子的情况,以便每个人都呈现正确的倍数?
所以case 0将是0n + 1,这样我就可以将所有帖子加载到这个结构中了?
我正在努力实现这种布局
(进行重复加载(稍后))
提前致谢!
答案 0 :(得分:1)
当然是男人!您的情况确实有简单的解决方案
你需要使用&#34;%&#34;算术运算符叫&#34;模块&#34;
$ a%$ b = $ a的剩余除以$ b
/**
* Example of % operator works
* 0 % 4 = 0 START
* 1 % 4 = 1
* 2 % 4 = 2
* 3 % 4 = 3
* 4 % 4 = 0 (RE)START
* 5 % 4 = 1
* 6 % 4 = 2
* 7 % 4 = 3
* 8 % 4 = 0 (RE)START
* ...
*/
while(...) :
/**
* Example. In your case $totalStylesNumber = 5
*/
$templateIndex = $count % $totalStylesNumber;
switch($templateIndex) {
case 0 : case 1:
include( locate_template( 'includes/homepage-1of2.php' ));
break;
case 3: case 4: case 5:
include( locate_template( 'includes/homepage-1of3.php' ));
break;
default: /** include case 2 */
include( locate_template( 'includes/homepage-1of1.php' ));
break;
}
$count++;
endwhile;
while(...) :
/**
* Example. In your case $totalStylesNumber = 5
*/
$templateIndex = $count % $totalStylesNumber;
/** case 3, case 4, case 5 */
if($templateIndex > 2)
include( locate_template( 'includes/homepage-1of3.php' ));
/** case 0, case 1 */
else if($templateIndex > -1 && $templateIndex != 2)
include( locate_template( 'includes/homepage-1of2.php' ));
/** default */
else
include( locate_template( 'includes/homepage-1of1.php' ));
$count++;
endwhile;