[英]add multiples to a php switch case
Super new to PHP and looked for duplicates but I might be searching for the wrong terminology 对PHP来说是超级新手,正在寻找重复项,但我可能正在寻找错误的术语
I have this query to load a different post template when running the query so I can load a more interesting page layout for a blog homepage 我有这个查询来在运行查询时加载其他帖子模板,因此我可以为博客首页加载更有趣的页面布局
<?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>
Is it possible to add an nth-child situation to the cases so that each renders itself on the correct multiple? 是否可以在个案中添加第n个子状况,以使每个个案呈现正确的倍数?
so case 0 would be 0n+1 so that I can load all of my posts into this structure? 所以案例0将是0n + 1,以便我可以将所有帖子加载到此结构中?
I am trying to achieve this layout 我正在尝试实现这种布局
(that carries on duplicating with a load more (later)) (进行更多负载复制(以后))
Thanks in Advance ! 提前致谢 !
Of course man! 当然可以! There are really simple solutions for Your situation
有针对您情况的简单解决方案
You need to use "%" arithmetic operator called "Module"
您需要使用称为“模块”的“%”算术运算符
$a % $b = Remainder of $a divided by $b
$ 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.