Display container for every two divs in while statment - php

I'm using foundation and Wordpress with Advanced Custom Field to build a services module.
I'm using the repeater field to insert the data.
Currently I have the row element outside of the while loop.
I need the div row to wrap every two "small-6 columns tgs-single-service" divs so the foundation grid displays properly. If I insert the row div in the while statement, it will wrap every single div in a row, which I do not want.
<?php if( have_rows('services_content') ):
$classNumber = 0
?>
<section class="full-width tgs-services-section">
<div class="row">
<?php while( have_rows('services_content') ): the_row(); ?>
<div class="small-6 columns tgs-single-service-<?php echo $classNumber = $classNumber + 1 ?>">
<figure class="effect-goliath">
<?php while( have_rows('service_images') ): the_row(); ?>
<img src="<?php the_sub_field('small_image');?>" srcset="<?php the_sub_field('medium_image');?> 1000w, <?php the_sub_field('large_image');?> 2000w" alt="Byoungz Poet Logo">
<?php endwhile; ?>
<figcaption>
<h3><?php the_sub_field('service_title');?></h3>
<?php the_sub_field('service_text');?>
</figcaption>
</figure>
</div><!-- end service section -->
<?php endwhile; ?>
</div><!--end row -->
</section>
<?php endif; ?>
Currently this displays as:
<div class="row">
<div class="small-6 columns tgs-service-section-1"></div>
<div class="small-6 columns tgs-service-section-2"></div>
<div class="small-6 columns tgs-service-section-3"></div>
<div class="small-6 columns tgs-service-section-4"></div>
</div>
I would like to achieve
<div class="row">
<div class="small-6 columns tgs-service-section-1"></div>
<div class="small-6 columns tgs-service-section-2"></div>
</div>
<div class="row">
<div class="small-6 columns tgs-service-section-3"></div>
<div class="small-6 columns tgs-service-section-4"></div>
</div>
Is there a way to have row wrap every two tgs-single-service divs?

<section class="full-width tgs-services-section">
<div class="row">
<!-- Create a counter i -->
<?php $i=1; while( have_rows('services_content') ): the_row(); ?>
<div class="small-6 columns tgs-single-service-<?php echo $classNumber = $classNumber + 1 ?>">
<figure class="effect-goliath">
<?php while( have_rows('service_images') ): the_row(); ?>
<img src="<?php the_sub_field('small_image');?>" srcset="<?php the_sub_field('medium_image');?> 1000w, <?php the_sub_field('large_image');?> 2000w" alt="Byoungz Poet Logo">
<?php endwhile; ?>
<figcaption>
<h3><?php the_sub_field('service_title');?></h3>
<?php the_sub_field('service_text');?>
</figcaption>
</figure>
</div><!-- end service section -->
<?php if($i % 2 == 0): ?><!-- Check if its divisable by 2 -->
</div>
<div class="row">
<?php endif; ?>
<?php $i++; ?>
<?php endwhile; ?>
</div><!--end row -->
</section>

Declare a counter, then determine if the count is a multiple of 2:
$i = 0;
//during your loop:
if(($i % 2) == 0){
//echo opening and closing tag
}

Please try this:
<?php
if( have_rows('services_content') ):
$classNumber = 0
$rowPerSection = 2;
$rows = get_field('services_content');
$sections = array_chunk($rows, $rowPerSection);
?>
<?php foreach ($sections as $section): ?>
<section class="full-width tgs-services-section">
<div class="row">
<?php foreach ($section as $row): ?>
# code...
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
<?php endif; ?>

Related

Looking to make archive.php display in two columns for Wordpress site

Very new Wordpress apprentice here. Trying to get my archive page to display posts in two columns going like:
Post 1 Post 2
Post 3 Post 4
Here is an example from a figma we were working on: https://ibb.co/N3XwtwD
My question is, what code can I add to my files to allow this? I currently have some bootstrap classes on the html portion I inserted here and it is displaying as one column, so I don't know if those classes will interfere with anything. Here is my archive.php code below:
<?php
get_header();
?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
while(have_posts()) {
the_post(); ?>
<div class="row">
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<img class="img-fluid mb-3"<?php
the_post_thumbnail();
?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
</div>
<?php }
echo paginate_links();
?>
</div>
<?php
get_footer();
?>
First time posting here so apologies if I left anything important out. Really appreciate this place!
Thanks!
You need to create a row every two posts and you have to add <div class="col-lg-6"> two times in a row. check below code.
<?php get_header(); ?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
$count = 1;
while( have_posts() ) { the_post();
if ( $count % 2 == 1){ ?>
<div class="row">
<?php } ?>
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<?php the_post_thumbnail(); ?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
<?php if ( $count % 2 == 0 ){ ?>
</div>
<?php } $count++; ?>
<?php }
if ( $count % 2 != 1 ) echo "</div>";
echo paginate_links(); ?>
</div>
Please replace with the below code that helps you.
<?php
get_header();
?>
<div class="container mt-5 mb-5">
<p class="font-size"><?php the_archive_title(); ?></p>
<hr>
</div>
<div class="container">
<?php
$counterval = 0;
while(have_posts()) {
the_post();
$counterval++;
if($counterval % 2 != 0)
{ ?>
<div class="row">
<?php } ?>
<div class="col-lg-6">
<p class="font-size text-center"><?php the_title();?></p>
<?php $featured_img_url = get_the_post_thumbnail_url(get_the_ID(),'full');
if(!empty($featured_img_url)){ ?>
<img class="img-fluid mb-3" src="<?php echo esc_url($featured_img_url); ?>" />
<?php } ?>
<p class="category-font">Published on <?php the_time('n.j.y');?></p>
<p>Posted by <?php the_author_posts_link() ?></p>
</div>
<?php
if($counterval % 2 == 0)
{ ?>
</div>
<?php } ?>
<?php } ?>
<?php echo paginate_links();
?>
</div>
<?php
get_footer();
?>

Use PHP and Bootstrap to make a column of content for ACF

Before I decided to post, I read all I could on this site with people of the same issues. Nothing seemed to work... I will explain it the best I can, I am using ACF + Repeater add-on to make A Restaurant Menu. I have Bootstrap loaded to help with making things easier, I want to have 3 columns going across. This is the HTML and PHP side of things.. I am using Bridge Theme, so I had to change the Bootstrap container class to container-acf because it kept going to the Bridge style instead. My end Goal if for it Look Similar To This All help is appreciated Thanks
My guess is I will need a Foreach loop.
`<?php
/*
Template Name: Restaurant Menu Template
*/
get_header(); ?>
<div class="content-fill-in-menu">HERE</div>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h1 class="entry-title">', '</h1>' ); ?>
</header><!-- .entry-header -->
<div class="entry-content wpb_wrapper container-acf">
<?php if ( have_rows('menu_sections') ):
while ( have_rows('menu_sections') ): the_row(); ?>
<h2 class="section_desc"><?php the_sub_field('section_title'); ?></h2>
<?php if ( have_rows('section_items') ): ?>
<?php while ( have_rows('section_items') ): the_row(); ?>
<article class="lmenu">
<ul>
<li>
<div class="container-acf">
<div class="row">
<div class="col-md-3 item_info">
<img class="dish_pic" src="<?php the_sub_field('dish_pic'); ?>" alt="<?php the_sub_field('dish_name'); ?>">
<h3 class="item_name"><?php the_sub_field('dish_name'); ?></h3>
<p class="item_desc"><?php the_sub_field('dish_description'); ?></p>
<h4 class="price">$<?php the_sub_field('dish_price'); ?></h4>
<span class="separator"></span>
</div>
</div>
</div>
</li>
</ul>
</article>
<?php endwhile; ?>
</table>
<?php endif; ?>
<?php endwhile;
endif; ?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
<?php endwhile; // End the loop. ?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<div class="content-fill-in-menu">HERE</div>
<?php get_footer(); ?>`
I think a lot of your problems are in the loop into "section_items" repeater field.
In the "while" loop, you open and close a container and a row for each item, so you don't have your render in columns.
Here in a idea of changes, you need open a row before the start of the loop. I put a counter ($i) to restart a row each 4 items to prevent problems :
<?php if ( have_rows('menu_sections') ): ?>
<?php while ( have_rows('menu_sections') ): the_row(); ?>
<h2 class="section_desc"><?php the_sub_field('section_title'); ?></h2>
<?php if ( have_rows('section_items') ): ?>
<?php $i = 1; ?>
<div class="row">
<?php while ( have_rows('section_items') ): the_row(); ?>
<div class="col-md-3 item_info">
<article class="lmenu">
<img class="dish_pic" src="<?php the_sub_field('dish_pic'); ?>" alt="<?php the_sub_field('dish_name'); ?>">
<h3 class="item_name"><?php the_sub_field('dish_name'); ?></h3>
<p class="item_desc"><?php the_sub_field('dish_description'); ?></p>
<h4 class="price">$<?php the_sub_field('dish_price'); ?></h4>
<span class="separator"></span>
</article>
</div><!-- /.col -->
<?php
if( $i == 4 ){
echo '</div><div class="row">';
$i = 0;
}
$i++;
?>
<?php endwhile; ?>
</div><!-- /.row -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>

how to query posts to insert in a row

So I managed to query my posts however they break/not aligned properly underneath after the first 2 columns. So I want to insert 2 posts per row, sorry if my explanation is really bad.
$loop = new WP_Query( array( 'post_type' => 'team') ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<!-- <div class="large-6 columns"> -->
<div class="row">
<div class="large-3 columns">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail($page->ID, 'medium'); ?>
<?php else: // use this image to fill the thumbnail ?>
<img src="http://placehold.it/350x150">
<?php endif; ?>
</div>
<div class="large-9 columns">
<div class="panel radius">
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
</div>
</div>
</div>
<!-- </div> -->
<?php endwhile; ?>
Not a zurb user, but i expect you need to start a new row every 2 posts:
$loop = new WP_Query( array( 'post_type' => 'team') );
$counter=0;
?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); $counter++;?>
<div class="row">
<div class="large-3 columns">
<?php if ( has_post_thumbnail()) : // Check if thumbnail exists ?>
<?php echo get_the_post_thumbnail($page->ID, 'medium'); ?>
<?php else: // use this image to fill the thumbnail ?>
<img src="http://placehold.it/350x150">
<?php endif; ?>
</div>
<div class="large-9 columns">
<div class="panel radius">
<h3><?php the_title(); ?></h3>
<p><?php the_content(); ?></p>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
</div>
</div>
</div>
<?php
//close row div and start another every 2 posts
if ($counter%2==0):?>
</div><div class="row">
<?php endif;?>
<?php endwhile; ?>

Why does this duplicate the posts outside as well as inside the while loop - PHP?

The following produces 2 posts in the loop with the surrounding div row-fluid. Then it creates another 2 posts after. Why is this?
EDIT:code above as requested:
<?php get_header(); ?>
<section class="clearfix" id="home">
<div class="row-fluid">
<?php $count = 1; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="span4">
<div class="wrap-title">
<div class="position">
<div class="title"><?php the_title();?></div>
<div class="tags"><?php echo $cats; ?> </div>
</div>
</div>
</div>
<?php if($count == 2) {
echo '</div>';
}?>
<?php $count++; ?>
<?php endwhile; ?>
I posted here as apposed to wordpress as I'm anticipating it being a PHP syntax error, something with the count variable possibly or where the row-fluid div is positioned.
Although I would create a code differently, the solution to your problem I believe is to modify your code to this:
<?php get_header(); ?>
<section class="clearfix" id="home">
<?php $count = 1; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($count % 2 == 0) {
<div class="row-fluid">
}?>
<div class="span4">
<div class="wrap-title">
<div class="position">
<div class="title"><?php the_title();?></div>
<div class="tags"><?php echo $cats; ?> </div>
</div>
</div>
</div>
<?php if($count % 2 == 0) {
echo '</div>';
}?>
<?php $count++; ?>
<?php endwhile; ?>
EDIT:
You said it produces 2 posts after also - that is why you will add </div> after first two posts, and then after that you will continue printing. The solution would be to use the code above - with % or to print only two posts use this code:
<?php get_header(); ?>
<section class="clearfix" id="home">
<div class="row-fluid">
<?php $count = 1; ?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="span4">
<div class="wrap-title">
<div class="position">
<div class="title"><?php the_title();?></div>
<div class="tags"><?php echo $cats; ?> </div>
</div>
</div>
</div>
<?php if($count == 2) {
break;
}?>
<?php $count++; ?>
<?php endwhile; ?>
</div>
But the above code could be optimized not to search for all posts, but it should will work as is.

Auto generate row with Twitter Bootstrap in Wordpress

Good morning, I have found myself in a bit of a dilemma! I am creating a Wordpress theme using Twitter Bootstrap and I am generating the members for the "Meet the Team" page via Wordpress "Posts" I can only fit 3 entries in 1 row... I.E
<div class="row">
<div class="span4"></div>
<div class="span4"></div>
<div class="span4"></div>
</div>
But anymore than 3 entries per a row will break the row, so I need to generate a new row for every 3 entries. How can I do this?
Here is my PHP code for outputting the entries.
<?php query_posts('category_name=members&orderby=date'); ?>
<div class="row-fluid">
<ul class="thumbnails">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<li class="span4">
<div class="thumbnail">
<?php // check if the post has a Post Thumbnail assigned to it.the_post_thumbnail();
?>
<div class="pad">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
</li>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
Try this
<?php query_posts('category_name=members&orderby=date'); ?>
<div class="row-fluid">
<ul class="thumbnails">
<?php
$cnt =0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
if($cnt%3==0){
echo "</ul></div><div class='row-fluid'><ul clsas='thumbnails'>";
}
?>
<li class="span4">
<div class="thumbnail">
<?php // check if the post has a Post Thumbnail assigned to it.the_post_thumbnail();
?>
<div class="pad">
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
</div>
</div>
</li>
<?php
$cnt++;
endwhile; ?>
</ul>
</div>
<?php endif; ?>

Categories