"If" loop on WordPress Page template repeats endlessly if post content contains anything

Sooo this damn custom page template is vexing me. I’m wondering if someone could look at the code and help troubleshoot why everything displays perfectly only when nothing is inputted into the post_content editor. When something (text, image, anything) is in the post_content editor, everything below the header (product-main-content, product-info (pricing/cart bar), related-products, etc.) is repeated infinitely. Help please!

Thanks so much!

<?php
global $product;

if ( has_post_thumbnail() ) {
	$full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full' );
}
?>

<div id="main" style="<?php echo $full_image_url ? 'background-image: url(\'' . $full_image_url[0] . '\');' : ''; ?>">

	<div class="product-main-content">
		<div id="detail">
			<div class="container-fluid">
				<div class="panel panel-product panel-product-<?php the_field('product_type'); ?>">
					<?php
					$field = get_field_object('coffee_type');
					$value = get_field('coffee_type');
					$label = $field['choices'][ $value ];
					?>

					<div class="panel-heading" style="background-color: #<?php echo $value; ?>">
						<h3 class="panel-title"><?php echo $label; ?></h3>
					</div>

					<div class="panel-body">
						<span class="product-subtitle"><?php the_field('product_label_subtitle'); ?></span>
						<h1 class="product-title"><?php the_title(); ?></h1>
						<span class="product-desc"><?php the_field('product_label_description'); ?></span>
					</div>
				</div><!-- /.panel -->
			</div>
		</div><!-- /#detail -->
	</div><!-- /.product-main-content -->

	<div class="product-info">
		<div class="product-info-1">
			<?php
			$post_object = get_field('featured_blog_post');

			if( $post_object ):

				// override $post
				$post = $post_object;
				setup_postdata( $post );

				?>
				<div class="featured-blog">
					<a href="<?php the_permalink(); ?>" class="btn btn-default">
						<span>"<?php the_title(); ?>"</span>
					</a>
				</div><!-- /.featured-blog -->
			    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
			<?php endif; ?>

			<?php // if(get_field('product_id')): ?>
			<?php if( $product->get_sku() && !empty($product->price) && $product->get_weight() ): ?>
			<div class="product-pricing">
				<form action="<?php echo esc_url( $product->add_to_cart_url() ); ?>" class="cart" method="post" enctype='multipart/form-data'>
					<input type="hidden" name="add-to-cart" value="<?php echo absint( $product->id ); ?>" />
                    <input type="hidden" name="product_id" value="<?php echo absint( $product->id ); ?>" />

					<div class="product-pricing-info">
						<span>$<?php echo $product->price; ?> <small>USD</small></span>
						<span><?php echo $product->get_weight() * 16; ?> <small>oz</small></span>
						<span><label for="qty">QTY</label> <span class="qty"><input type="number" name="quantity" id="qty" value="1" min="1"></span></span>
					</div><!-- /.product-pricing-info -->

					<div class="add-to-cart-container Cart66CartButton">
						<button type="submit" value="Add To Cart" class="purAddToCart ajax-button">Add To Cart</button>
					</div><!-- /.add-to-cart-container -->
				</form>
			</div><!-- /.product-pricing -->
			<?php else: ?>
				<div class="product-pricing no-product">
					<div class="product-pricing-info">
						<span class="not-available-msg">Unavailable for online purchase</span>
					</div>
				</div>
			<?php endif; ?>
		</div><!-- /.product-info-1 -->

		<?php if(get_field('product_facts')) : ?>
		<div class="product-facts">
			<?php
			$post_objects = get_field('product_facts');

			if( $post_objects ): ?>
			    <ul class="list-unstyled">
			    <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
			        <?php setup_postdata($post); ?>
			        <li>
			            <?php the_title(); ?>
			        </li>
			    <?php endforeach; ?>
			    </ul>
			    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
			<?php endif; ?>
		</div><!-- /.product-facts -->
		<?php endif; ?>
	</div><!-- /.product-info -->

</div><!-- /#main -->
	
<?php if($post->post_content != "") : ?>
<section id="content" role="main">
	<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
	<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
		<header class="header">
			<div class="container-fluid">
				<h2>
					About the coffee: <span class="highlight"><?php the_title(); ?>.</span>
				</h2>
				<?php edit_post_link(); ?>
			</div><!-- /.container-fluid -->
		</header>
		<div class="container-fluid">
			<section class="entry-content">
				<?php the_content(); ?>
				<div class="entry-links"><?php wp_link_pages(); ?></div>
			</section>
		</div><!-- /.container-fluid -->
	</article>
	<?php if ( ! post_password_required() ) comments_template( '', true ); ?>
	<?php endwhile; endif; ?>
</section>
<?php endif; ?>

<?php

// check if the repeater field has rows of data
if( have_rows('related_products') ): ?>

<div id="related-products">
	<div class="container-fluid">
		<h2>Other Products You Might be Interested In</h2>

		<div class="row">

		 	<?php // loop through the rows of data
		    while ( have_rows('related_products') ) : the_row();

		    $related_product = get_sub_field('related_product');
		    ?>

			<div class="col-md-6">
				<a href="<?php echo get_the_permalink($related_product->ID); ?>" class="product-link">
					<div class="panel panel-product panel-product-<?php the_field('product_type', $post->ID); ?>">
						<?php
						$field = get_field_object('coffee_type', $related_product->ID);
						$value = get_field('coffee_type', $related_product->ID);
						$label = $field['choices'][ $value ];
						?>

						<div class="panel-heading" style="background-color: #<?php echo $value; ?>">
							<h3 class="panel-title"><?php echo $label; ?></h3>
						</div>

						<div class="panel-body">
							<span class="product-subtitle"><?php the_field('product_label_subtitle', $related_product->ID); ?></span>
							<h1 class="product-title"><?php echo get_the_title($related_product->ID); ?></h1>
							<span class="product-desc"><?php the_field('product_label_description', $related_product->ID); ?></span>
						</div>
					</div><!-- /.panel -->
				</a>
			</div>

		    <?php endwhile; ?>

	    </div>

	</div>
</div><!-- /#related-products -->
	
<?php else :
	
		// no rows found

endif;

?>

<?php get_footer(); ?>

this might be the problem. once you post, with while loop it will forever be true because the post occured. you can change to if statement instead.

Thanks for the response! It’s not that however :frowning: That bit of code shows up in the single blog post template as well. I also tried removing it anyway and it still loops :frowning:
Any other thoughts?

use foreach instead?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.