admin管理员组文章数量:1334160
I created a loop in my archive page displaying the posts in 3 columns (33% width) inside a row with 100% width. Each column is a post.
What I would like to achieve is to make the loop more flexible for smaller screens: 2 columns of 50% inside the row container for tablets then 1 column of 100% for mobiles. My main problem is, I need the posts inside the row container. So It would be: a row with 3 columns, then a row with 2 columns and a row with 1 column.
This is the code of the loop:
<!-- Start the Loop -->
<div class="row">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'archive' ); ?>
<?php if( $wp_query->current_post % 3 == 2 ) : ?>
</div>
<!-- row -->
<div class="row">
<?php endif; ?>
<?php endwhile; ?>
Any help would be much appreciated, thanks!
I created a loop in my archive page displaying the posts in 3 columns (33% width) inside a row with 100% width. Each column is a post.
What I would like to achieve is to make the loop more flexible for smaller screens: 2 columns of 50% inside the row container for tablets then 1 column of 100% for mobiles. My main problem is, I need the posts inside the row container. So It would be: a row with 3 columns, then a row with 2 columns and a row with 1 column.
This is the code of the loop:
<!-- Start the Loop -->
<div class="row">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'archive' ); ?>
<?php if( $wp_query->current_post % 3 == 2 ) : ?>
</div>
<!-- row -->
<div class="row">
<?php endif; ?>
<?php endwhile; ?>
Any help would be much appreciated, thanks!
Share Improve this question edited Jun 9, 2020 at 7:41 Mathieu Préaud asked Jun 9, 2020 at 7:35 Mathieu PréaudMathieu Préaud 2035 silver badges18 bronze badges1 Answer
Reset to default 0You can control this with media queries in CSS.
@media screen and (max-width: 768px) { div.row { width: 50%; } } /* for iPad */
@media screen and (max-width: 600px) { div.row { width: 100% } } /* for mobile phones */
I hope this helps.
UPDATE: You can't do this with PHP because PHP is executed on the server before the content is delivered to the browser, so there is no time for PHP to determine which device or viewport is in use. Have a look here.
本文标签: phpResponsive loop with 3 columns inside row then 2 columns
版权声明:本文标题:php - Responsive loop with 3 columns inside row then 2 columns 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742364378a2460990.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论