My code causing infinite loop, can't see the bug - php

I am working on a WordPress project, using the Advanced Custom Fields plugin.
However I am getting a crazy infinite loop from my code, so I've obviously messed things up somewhere, however after a good two hours looking at it I can't see the problem.
<?php get_header(); ?>
<?php
// If the loop has posts
if (have_posts()) :
// For each post this page has
while (have_posts()) : the_post();
// Display the Flexible Content
while(has_sub_field('add_row')):
// If the content type is Tagline
if(get_row_layout() == 'tagline'): ?>
<div class="row paddingBottom paddingTop">
<div class="col-xs-12">
<h3 class="tagLine"><?php the_sub_field('tagline'); ?></h3>
</div>
</div>
<?php
// If the content type is a Featured Image
/*elseif ( get_row_layout() == 'featured_image' ): ?>
<div class="col-xs-12 textCenter">
<?php
$attachment_id = get_sub_field('featured_image');
$size = "full-width"; // (thumbnail, medium, large, full or custom size)
$image = wp_get_attachment_image_src( $attachment_id, $size );
?>
<img src="<?php echo $image[0]; ?>" alt="<?php the_title(); ?>" />
</div>
<?php*/
// If the content type is a horizontal rule
elseif ( get_row_layout() == 'horizontal_rule' ) : ?>
<div class="row">
<div class="col-xs-12">
<hr />
</div>
</div>
<?php
// If the content type is Columns
elseif ( get_row_layout() == 'columns' ):
// If there is at least one column section
if(get_sub_field('column')):
// For each column section
while(has_sub_field('column')): ?>
<div class="row paddingTop paddingBottom">
<?php
if(get_sub_field('column_content_type')):
$count_rows = 0;
// Counting
while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
// Loop
while(has_sub_field('column_content_type')): ?>
<div class="
<?php /*
if ( $count_rows == 1 ) :
echo "col-xs-12";
elseif ( $count_rows == 2 ) :
echo "col-xs-6";
elseif ( $count_rows == 3 ) :
echo "col-xs-6 col-sm-4";
elseif ( $count_rows == 4 ) :
echo "col-xs-6 col-sm-6 col-md-4";
endif; */
?>
"> <?php
// Title
if ( get_sub_field('title') ) : ?>
<h2 class="smallTitle"><?php the_sub_field('title');?></h2><?php
endif;
// Text Area
if ( get_sub_field('text') ) :
the_sub_field('text');
endif;
// Link
if ( get_sub_field('link') ) : ?>
<?php // eventually need to make it so you can change the link text ?>
<a class="textCenter" href="<?php the_sub_field('link'); ?>">
More info
</a> <?php
endif; ?>
</div> <?php
endwhile;
endif;
?>
</div>
<?php
endwhile; // end for each column section
endif; // end checking for at least one column section
endif; // end checking content type is columns
endwhile; // end flexible content
endwhile; // end for each post
endif; // end checking for at least one post
?>
<?php get_footer(); ?>

while(has_sub_field('column_content_type')):
$count_rows++;
endwhile;
That's your problem... But it could be one of many based on what I'm seeing. It looks like you think while loops work like if statements... All you are doing in that loop is incrementing it over and over. It will never end.

I think the mistake is
// Display the Flexible Content
while(has_sub_field('add_row')):
i think you need to use if(){
// Code
}
instead of "while"

Related

Use a while loop to display unlimited data in a 5 column Bootstrap Carousel

Is there a way to create a Bootstrap Carousel with 5 columns and use a while loop to output all the images?
How would it work with rows and columns?
This is my while loop:
<?php if( have_rows('logos') ): ?>
<?php while( have_rows('logos') ): the_row();
// vars
$image = get_sub_field('logo_image');
$content = get_sub_field('name');
?>
<div class="item active">
<img src="<?php echo $image['url']; ?>">
<?php endwhile; ?>
<?php endif; ?>
Here is the example :
<?php if( have_rows('logos') ): ?>
<div class="row">
<?php while( have_rows('logos') ): the_row();
// vars
$image = get_sub_field('logo_image');
$content = get_sub_field('name');
?>
<div class="item active col-2">
<img src="<?php echo $image['url']; ?>">
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
Bootstrap doc:
use .col- for Extra small <576px ==> Example : col-2 ,
col-6
use .col-sm- for Small ≥576px ==> Example : .col-sm-2
,.col-sm-6
use .col-md- for Medium ≥768px ==> Example : .col-md-2
,.col-md-6
use .col-lg- for Large ≥992px ==> Example : .col-lg-2
,.col-lg-6
use .col-xl- for Extra large ≥1200px ==> Example : .col-xl-2
, .col-xl-6

ACF Repeater in Wordpress not showing images

I'm having trouble getting images shown with a repeater in Wordpress.
For some reason it only shows the alt text instead of the image itself?
example
the upper images are my thumbnail posts and are showing correctly.
Does anyone know whats causing this?
my code
<div class="row">
<div class="col-sm-6 left"><h1>COMMERCIAL</h1><br>
<div class="page-header">
<?php $mymovies = new WP_Query(array(
'post_type' => 'my_movies'
)); ?>
<?php while($mymovies->have_posts()) : $mymovies->the_post(); ?>
<div class="movie-colums">
<div class="thumbtitle group">
<div class="preview">
<?php the_post_thumbnail('thumbnail'); ?>
<h1><?php the_title(); ?><br>
<?php the_field('year'); ?>
<?php
// check if the repeater field has rows of data
if( have_rows('images_rep') ):
// loop through the rows of data
while ( have_rows('images_rep') ) : the_row();
// display a sub field value
the_sub_field('image');
endwhile;
else :
// no rows found
endif;
?>
</h1>
</div>
</div>
</div>
<?php endwhile; ?>
</div>
thanks in advance
Your PHP bit for the repeater stuff should be:
<?php if( have_rows('images_rep') ): ?>
<?php while( have_rows('images_rep') ): the_row();
$image = get_sub_field('image');
?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>">
<?php endwhile; ?>
<?php endif; ?>
It looks like your example is outputting the image object so just wrap the image URL in an image tag.
Ref: https://www.advancedcustomfields.com/resources/image/

using an if statement inside a while with wordpress

Hi guys I'm using a while loop to display my posts and while I have posts I want to display the odd and even post numbers differently. I currently have the following code:
<div class="container">
<?php if ( function_exists('yoast_breadcrumb') ) {
yoast_breadcrumb('<p id="breadcrumbs">','</p>');
} ?>
</div>
<?php query_posts('post_type=portfolio&post_status=publish&posts_per_page=8&paged='. get_query_var('paged'));
if( have_posts() ):
echo '<div>';
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) :
$wp_query->the_post();
$i = 0;
if($i % 2 == 0)
{?>
<div class="class1">
html content 1 to go here
</div>
<?php
$i++;
}else{
?>
<div class="class2">
html content 2 to go here
</div>
<?php $i++; } ?>
<?php endif; ?>
<?php endwhile; ?>
<div class="container">
<div class="navigation">
<span class="newer"><?php previous_posts_link(__('« See Previous Case Studies','example')) ?></span>
<span class="older"><?php next_posts_link(__('See Next Case Studies »','example')) ?></span>
</div>
</div> <!-- /.navigation -->
<?php else: ?>
<div id="post-404" class="noposts">
<p><?php _e('None found.','example'); ?></p>
</div><!-- /#post-404 -->
<?php endif; wp_reset_query(); ?>
I haven't used php for a while, so I'm unsure the best way to do the above. I'm currently getting a few errors relating to syntax such as unexpected end of file or unexpected } on line...
Can anyone figure out where I am going wrong?
Reverse Position
<?php endwhile; ?>
<?php endif; ?>
First While statement should get complete than if.

PHP - Adding divs to a foreach loop every 4 times, working with $post_object

I am building a wordpress page (based on quark, using ACF) showing info from other posts using foreach(get_field('exhibitions') as $post_object)
I want the 'col grid_3_of_12-divs' containing info from other posts to group up in divs, with four in each div. A similar problem as the author of this question had: Is there an easy way to do 4 at a time in a php foreach loop
I have tried with array_chunk, but can't seem to make it without messing with the retrieving of info from the other posts.
Can someone help me with this?
I am a self taught php beginner, so please excuse me if my code is stupid..
<?php get_header(); ?>
<div id="primary" class="site-content row clearfix" role="main">
<div class="col grid_12_of_12">
<?php while ( have_posts() ) : the_post(); ?>
<?php foreach(get_field('exhibitions') as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div class="col grid_3_of_12">
<h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>
<?php $attachment_id = $post_object->thumbnail;
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
<img src="<?php echo $image_attributes[0]; ?>">
<p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
</div>
</a>
<?php endforeach; ?>
<?php content_nav( 'nav-below' ); ?>
<?php endwhile; ?>
</div>
</div>
Add a counter. Then when the counter is at the right spot, add a close or open div.
<?php get_header(); ?>
<div id="primary" class="site-content row clearfix" role="main">
<div class="col grid_12_of_12">
<?php while ( have_posts() ) : the_post(); ?>
<?php $counter = 0; /* ADD THIS */ ?>
<?php foreach(get_field('exhibitions') as $post_object): ?>
<?php if ($counter % 4 == 0): /* ADD THIS */ ?>
<div class="group-of-4-posts-wrapper">
<?php endif; ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div class="col grid_3_of_12">
<h3 class="exhibition title"> <?php echo $post_object->short_title?> </h3>
<?php $attachment_id = $post_object->thumbnail;
$image_attributes = wp_get_attachment_image_src( $attachment_id, 'full' ); ?>
<img src="<?php echo $image_attributes[0]; ?>">
<p class="exhibition short desc"> <?php echo $post_object->short_description?> </p>
</div>
</a>
<?php if ($counter % 4 == 3): /* ADD THIS */ ?>
</div>
<?php endif; ?>
<?php $counter++ ?>
<?php endforeach; ?>
<?php
// this closes the div if there is not a number of posts that is evenly
// divisable by 4, like 11 posts. with 11 posts, the last post would have
// ($counter % 4 == 3) equal to false, because $counter % 4 would = 2
// adding this, closes the div, if it was not already closed
if ($counter % 4 != 0): /* ADD THIS. */
?>
</div>
<?php endif; ?>
<?php content_nav( 'nav-below' ); ?>
<?php endwhile; ?>
</div>
</div>

Find oldest post — WordPress

Thanks to some answers on here, I've managed to distinguish my posts into the latest post and all the rest. However, it needs to be the oldest post. Here is my current loop:
<?php if (have_posts()) : ?>
<?php $post = $posts[0]; $c=0;?>
<?php while (have_posts()) : the_post(); ?>
<!-- first post -->
<?php $c++;
if( $c == 1) :?>
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="down">a</div>
</div>
</div>
<?php else :?>
<!-- second post -->
<div class="container">
<div class="inner_box">
<ul>
<div class="title">
<a href="<?php the_permalink();?>">
<?php the_title(); ?>
</div>
<?php the_content(); ?>
</a>
</ul>
<div class="up">b</div>
</div>
</div>
<?php endif; ?>`
I saw somewhere that you can use a while loop to target the last post from a post.length. However, I am unsure how to implement this.
Yes, you're right. Use count.
Suppose the total posts is 5 for $total_posts = count($posts);.
You'll have to check your $counter for total - 1 as the array is $posts[0], $posts[1], $posts[2], $posts[3], $posts[4].
<?php
if ( have_posts() ) :
$counter = 0;
$total_posts = count($posts) - 1;
while ( have_posts() ) :
the_post();
if( $counter == $total_posts ) {
?>
<div class="container"><?php // CUSTOM CODE FOR LAST ARTICLE ?></div>
<?php
} else {
?>
<div class="container"><?php // CUSTOM CODE FOR THE REST OF ARTICLES ?></div>
<?php
}
$counter++;
endwhile;
endif;
Note that you only need to open <?php and close ?> when changing from PHP to HTML, using them at every line is very confusing.

Categories