Home › Forums › Front-end Issues › ACF while loop with have_rows is breking if foreach loop is present inside it
Wordpress Version: 5.0.3
ACF Pro Version : 5.7.9
Home Template1 Code
<?php
/* Template Name: Home Template1 */
get_header();
if(have_rows('home_page_builder')):
echo '<br>Inisde if';
while(have_rows('home_page_builder')): the_row();
$layoutName=get_row_layout();
get_template_part('template-parts/home/template1/flexiblelayout',$layoutName);
endwhile;
endif;
get_footer();
Template parts code
flexiblelayout-latest_news.php
<?php
$args = array(
'post_type' => array( 'post'),
'post_status'=>'publish',
'posts_per_page' => 11,
'orderby' => 'date ID',
'order' => 'DESC'
);
$latestNews = new WP_Query( $args );
//print_r($latestNews);
$colsmPosts=$colmdPosts=$colxsPosts=array();
if($latestNews->have_posts())
{
$cnt=1;
while($latestNews->have_posts())
{
$latestNews->the_post();
$postId=get_the_ID();
$postCat=get_the_category($postId);
if($cnt>=1 && $cnt<3){
$colmdPosts[$postId]=[
'title'=>get_the_title(),
'permalink'=>get_the_permalink(),
'thumbnail'=>get_the_post_thumbnail_url(null,'thumbnail-lg-500-x-287'),
'excerpt'=>wolftechnewsmaster_get_excerpt(70),
'date'=>get_the_date(),
'comments_number'=>get_comments_number(),
'category'=>$postCat[0]->name,
'category_permalink'=>$postCat[0]->slug
];
}
else if($cnt>=3 && $cnt<6)
{
$colsmPosts[$postId]=[
'title'=>get_the_title(),
'permalink'=>get_the_permalink(),
'thumbnail'=>get_the_post_thumbnail_url(null,'thumbnail-md-240-x-160'),
'excerpt'=>wolftechnewsmaster_get_excerpt(60),
'date'=>get_the_date(),
'comments_number'=>get_comments_number(),
'category'=>$postCat[0]->name,
'category_permalink'=>$postCat[0]->slug
];
}
else if($cnt>=6)
{
$colxsPosts[$postId]=[
'title'=>get_the_title(),
'permalink'=>get_the_permalink(),
'thumbnail'=>get_the_post_thumbnail_url(null,'thumbnail-sm-100-x-85'),
'excerpt'=>wolftechnewsmaster_get_excerpt(60),
'date'=>get_the_date(),
'comments_number'=>get_comments_number(),
'category'=>$postCat[0]->name,
'category_permalink'=>$postCat[0]->slug
];
}
$cnt+=1;
} wp_reset_postdata();
//print_r([$colsmPosts,$colmdPosts,$colxsPosts]);
}
// echo '-----------------------';
?>
<!-- latest news -->
<section class="latest-area pb-30">
<div class="container">
<div class="row">
<div class="col-12">
<div class="news-block-title mb-30">
<h2>Latest News</h2>
<div class="news-block-bar f-right" title="थप समाचार">
<a class="info-bar" href="#">
<i class="fas fa-bars" style="margin:12px;"></i>
</a>
</div>
</div>
</div>
</div>
</div>
<div class="container">
<div class="row row-10">
<div class="col-20">
<?php
foreach ($colsmPosts as $id=>$post)
{ ?>
<div class="postbox mb-25">
<div class="postbox__thumb">
<a href="<?php echo $post['permalink']; ?>">
<img src="<?php echo $post['thumbnail']; ?>" alt="<?php echo $post['title']; ?>">
</a>
<span class="post-cat post-absolute">
<a href="<?php echo $post['category_permalink']; ?>"><?php echo $post['category']; ?></a>
</span>
</div>
<div class="postbox__text pt-10">
<div class="postbox__text-meta pb-10">
<ul>
<li>
<i class="fas fa-calendar-alt"></i>
<span><?php echo $post['date']; ?></span>
</li>
<li>
<i class="far fa-comment"></i>
<span>(<?php echo $post['comments_number']; ?>)</span>
</li>
</ul>
</div>
<h4 class="pr-0">
<a href="<?php echo $post['permalink']; ?>"><?php echo $post['title']; ?></a>
</h4>
</div>
</div>
<?php }
?>
</div>
<div class="col-40">
<?php
foreach ($colmdPosts as $id=>$post)
{ ?>
<div class="postbox mb-25">
<div class="postbox__thumb">
<a href="<?php echo $post['permalink']; ?>">
<img src="<?php echo $post['thumbnail']; ?>" alt="<?php echo $post['thumbnail']; ?>">
</a>
<span class="post-cat post-absolute">
<a href="<?php echo $post['category_permalink']; ?>"><?php echo $post['category']; ?></a>
</span>
</div>
<div class="postbox__text pt-10">
<div class="postbox__text-meta pb-10">
<ul>
<li>
<i class="fas fa-calendar-alt"></i>
<span><?php echo $post['date']; ?></span>
</li>
<li>
<i class="far fa-comment"></i>
<span>(<?php echo $post['comments_number']; ?>)</span>
</li>
</ul>
</div>
<h4 class="pr-0">
<a href="<?php echo $post['permalink']; ?>"><?php echo $post['title']; ?></a>
</h4>
</div>
</div>
<?php }
?>
</div>
<div class="col-20 d-md-none d-xl-block">
<?php
foreach ($colxsPosts as $id=>$post)
{ ?>
<div class="post__small mb-30">
<div class="post__small-thumb f-left">
<a href="<?php echo $post['permalink']; ?>">
<img src="<?php echo $post['thumbnail']; ?>" alt="<?php echo $post['title']; ?>">
</a>
</div>
<div class="post__small-text fix pl-10">
<span class="sm-cat">
<a href="<?php echo $post['category_permalink']; ?>"><?php echo $post['category']; ?></a>
</span>
<h4 class="title-13 pr-0">
<a href="<?php echo $post['permalink']; ?>"><?php echo $post['title']; ?></a>
</h4>
<div class="post__small-text-meta">
<ul>
<li>
<i class="fas fa-calendar-alt"></i>
<span><?php echo $post['date']; ?></span>
</li>
</ul>
</div>
</div>
</div>
<?php }
?>
</div>
<div class="col-20 ">
<div class="nb-sidebar-add-block">
<?php
$addsBlock = get_sub_field('choose_adds_block');
$addsBlockFieldName=$addsBlock['value'];
//print_r($addsBlock);
$adds=get_field($addsBlockFieldName,'option');
foreach($adds as $add)
{
//print_r($add);
$addUrl=get_the_post_thumbnail_url($add->ID);
?>
<div class="nb-sidebar-add">
<a href="#">
<img class="img-fluid" src="<?php echo $addUrl; ?>" alt="<?php echo $add->post_title; ?>">
</a>
</div>
<?php
}
?>
</div>
</div>
</div>
</div>
</section>
<!-- latest news end -->
Problem:
Inside flexiblelayout-latest_news.php if I remove all foreach loop then it will loads all other template parts but when foreach loop is present their only this template part is loded…
I couldnt figure out what is breaking the while …. any help is highly appreciated
Thank you
The topic ‘ACF while loop with have_rows is breking if foreach loop is present inside it’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.