foreach statement within ACF repeater - php

I'm not sure if I'm doing this correctly? I have a nested ACF repeater, which is working fine, but the section are all displaying as one lone block. I want to be able to break the blocks into columns. So I though to do this I would need a "foreach" statement so I could assign an id or class to each section, allowing me to be able to style each section individually - float and add margins etc.
I can't quite get it to work!
<div class="col-xl-12 food-section">
<?php
$rows = get_field('section_container');
if($rows) {
foreach($rows as $row):
?>
<div class="col-xl-6 section-test"> <?php
if( have_rows('section_container') ) :
while( have_rows('section_container') ): the_row(); ?>
<h1><?php the_sub_field('section_heading'); ?></h1>
<div class="section-image"><?php
$image = get_sub_field('section_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
</div>
<?php endif;
if( have_rows('sub_section_container') ):
while( have_rows('sub_section_container') ): the_row();
?>
<h2><?php the_sub_field('sub_section_heading'); ?></h2>
<?php
if( have_rows('food_item') ):
while( have_rows('food_item') ): the_row();
?>
<div id="menu-table">
<h3 id="leftside"><?php the_sub_field('item_name'); ?></h3>
<h4 id="rightside"><?php the_sub_field('price'); ?></h4>
<div id="menu-description"><p><?php the_sub_field('item_description'); ?></p></div>
<div class="stroke"></div>
</div>
<?php endwhile; //food_item
endif; //food_item
endwhile; //section_h1_container
endif; //section_h1_container
endwhile; //section_container
endif; //section_container
wp_reset_postdata();
endforeach; }
?></div>
</div><!-- food-section -->

I've cleaned up your code, and I'm hoping I'm understanding your request. You had an unnecessary if and foreach at the top of the snippet, and a wp_reset_postdata() that is not needed at the end of a repeater call in ACF.
You can read more on nested repeaters here:
https://www.advancedcustomfields.com/resources/working-with-nested-repeaters/
<?php if( have_rows('section_container') ) : ?>
<div class="col-xl-12 food-section">
<?php while( have_rows('section_container') ): the_row(); ?>
<div class="col-xl-6 section-test">
<h1><?php the_sub_field('section_heading'); ?></h1>
<div class="section-image">
<?php $image = get_sub_field('section_image');
if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
</div>
<?php if( have_rows('sub_section_container') ):
while( have_rows('sub_section_container') ): the_row(); ?>
<h2><?php the_sub_field('sub_section_heading'); ?></h2>
<?php if( have_rows('food_item') ):
while( have_rows('food_item') ): the_row(); ?>
<div id="menu-table">
<h3 id="leftside"><?php the_sub_field('item_name'); ?></h3>
<h4 id="rightside"><?php the_sub_field('price'); ?></h4>
<div id="menu-description"><p><?php the_sub_field('item_description'); ?></p></div>
<div class="stroke"></div>
</div>
<?php endwhile; //food_item
endif; //food_item
endwhile; //section_h1_container
endif; //section_h1_container ?>
</div>
<?php endwhile; //section_container ?>
</div>
<?php endif; //section_container ?>
This snippet checks if any rows are set for "section_container" if so, it creates the "food-section" div. It then loops through each row of "section_container". For each row, it creates a "section-test" div, and begins to populate it with the subfield data for that row. It then checks for the "sub_section_container" repeater inside your current row, and loops through the rows inside that nested repeater, and outputs the data.

Related

Wrap every 2 posts on a row

I'm using Bootstrap, I have a list of posts and I want to wrap every 2 posts on a row. Each post is wrapped on a <div col>. You can see live here.
I tried with this but it wrap the row each one post:
<?php
$num = 0;
// Check if there are any posts to display
if ( have_posts() ) : ?>
<?php
// The Loop
while ( have_posts() ) : the_post();
if($num%2) {
echo "<div class='row' style='margin-bottom: 2.3em;''>";
}
?>
<div class="col-xs-12 col-sm-6 col-md-6">
<h2 class="category-encabezado">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Enlace a <?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</h2>
<small>Hace
<?php echo human_time_diff( get_the_time('U'), current_time('timestamp') ) . ''; ?>
</small>
<div class="entry">
<p>
<?php the_excerpt(); ?>
</p>
<?php
$my_shortcode = get_field('audio-field');
echo do_shortcode( $my_shortcode );
?>
</div>
</div>
<?php
if($num %2) {
echo '</div>';
}
$num++
?>
<?php endwhile; // End Loop
?>
</div>
<?php
You have to put div.row Out of the Loop while

Display total number of items from while loop outside of it php

I have a PHP while loop and i'm trying to use a counter to display the total number of items (.question) that are within it. I'm trying to echo the contents of $child_i outside of the while loop to do this but the code below doesn't work. Whats the best way of achieving this?
EDIT
Updated to show full code
<?php if( have_rows('faqs') ): ?>
<ul class="responsive-accordion responsive-accordion-default bm-larger">
<?php while( have_rows('faqs') ): the_row(); ?>
<li>
<div class="responsive-accordion-head"><span class="ico arrow-right"></span><?php the_sub_field('category_name'); ?> <span class="faq-counter">
<!-- TRYING TO ECHO $CHILD_I ON THE LINE BELOW BUT NOT WORKING -->
<?php echo($child_i); ?> Questions</span></div>
<!-- $CHILD_I IS DEFINED ON THE NEXT LINE -->
<?php if( have_rows('questions') ): $child_i = 0; ?>
<!-- THIS ECHO WORKS -->
<div class="responsive-accordion-panel <?php echo($child_i); ?>">
<?php while( have_rows('questions') ): the_row(); ?>
<div class="question">
<h6><?php the_sub_field('question'); ?></h6>
<p><?php the_sub_field('answer'); ?></p>
</div>
<?php $child_i++; endwhile; ?>
</div>
<?php endif; //if( get_sub_field('questions') ): ?>
</div>
</li>
<?php endwhile; // while( has_sub_field('to-do_lists') ): ?>
</ul>
<?php endif; // if( get_field('to-do_lists') ): ?>
Try this
$questions = get_field('questions'); $size=count($questions); echo $size;

Having trouble displaying custom post meta via a short code in a WordPress plugin

I've created a plugin that sets up a custom post type ('Programmes'), taxonomy and a short code to display the custom posts. I'm trying to display a custom post meta field (Broadcast Date) via the shortcode. These are the methods I've tried so far:
This is the method I'd have preferred as it allows me to use it for multiple meta values:
if ( $query->have_posts() ) { ?>
<div class="schedule-container">
<?php while ( $query->have_posts() ) : $query->the_post();
$programmeImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
$programmeMeta = get_post_meta($post->ID,'_meta_content',TRUE);
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url( <?php echo $programmeImage ?> )">
<div class="schedule-titlebar">
<h3><?php the_title(); ?></h3>
<p><?php echo $programmeMeta['broadcast-date'] ?></p>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
}
But that didn't work so I tried this instead:
if ( $query->have_posts() ) { ?>
<div class="schedule-container">
<?php while ( $query->have_posts() ) : $query->the_post();
$programmeImage = wp_get_attachment_url( get_post_thumbnail_id($post->ID, 'thumbnail') );
$programmeBroadcastDate = get_post_meta($post->ID,'broadcast-date',TRUE);
?>
<div id="programme-<?php the_ID(); ?>" <?php post_class(); ?> style="background-image: url( <?php echo $programmeImage ?> )">
<div class="schedule-titlebar">
<h3><?php the_title(); ?></h3>
<p><?php echo $programmeBroadcastDate ?></p>
</div>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
</ul>
}
and that doesn't work either!
Any ideas? Thanks

pagination custom post type with multiple WP_Query loop

after adding the function of pagination to Functions.php and recall it in template-product-listing.php
there is nothing shown in result.
I have a big problom with this...
could you find and resolve the problem?
thnx
<article class="row">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="single-head" class="sixteen columns">
<h1><?php the_title(); ?></h1>
<?php if (has_excerpt()) { the_excerpt(); } ?>
</div>
<div class="row">
<nav id="portfolio-filters" class="sixteen columns">
<?php _e('Show All', 'ci_theme'); ?>
<?php
$args = array(
'hide_empty' => 0
);
$skills = get_terms('product-category', $args);
?>
<?php foreach ( $skills as $skill ) : ?>
<?php echo $skill->name; ?>
<?php endforeach; ?>
</nav><!-- /portfolio-filters -->
</div>
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query('post_type=product&posts_per_page=4'); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
<?php $item_skills = wp_get_object_terms($post->ID, 'product-category'); ?>
<article class="<?php ci_e_setting('product_columns'); ?> columns <?php foreach ( $item_skills as $item_skill ) : echo $item_skill->slug.' '; endforeach; ?> columns portfolio-item">
<a href="<?php echo get_permalink(); ?>" title="<?php echo esc_attr(get_the_title()); ?>" class="fb">
<?php the_post_thumbnail('ci_portfolio_slider', array('class'=>'scale-with-grid')); ?>
</a>
<div class="portfolio-desc">
<h3><?php the_title(); ?></h3>
<p class="desc"><?php echo mb_substr(get_the_excerpt(), 0, 70); ?>...</p>
</div>
</article><!-- /portfolio-item -->
<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
</div><!-- /portfolio-items -->
<?php get_template_part('part', 'call_to_action'); ?>
<?php endwhile; endif; ?>
<div class="pagination">
<?php wp_pagination(); ?>
</div>
</article>
if your sure there is a post_type called products and there is posts in it...
try:
<div id="portfolio-items" class="row">
<?php $ci_product_query = new WP_Query(array('post_type'=>'product', 'posts_per_page'=> 4); ?>
<?php if ( $ci_product_query-> have_posts() ) : while ( $ci_product_query->have_posts() ) : $ci_product_query->the_post(); ?>
You know you are starting a new wp_query for every post in have_posts() ? you might want to rethink what you are trying to achieve!

Wordpress: Check posts have comments allowed

I have a page that displays certain posts from a certain category, in this case category 33 (Tutorials) and it currently outputs the title, post excerpt and permalink to the posts in this category:
<?php $top_query = new WP_Query('cat=33'); ?>
<?php while($top_query->have_posts()) : $top_query->the_post(); ?>
How Can I specify that the posts returned should only be ones that have comments enabled?. I have tried wrapping it in:
<?php if(comments_open()) : ?>
Hover that needs to be used within the loop :(
Thanks in advance
try this one
<?php if( have_posts() ): ?>
<?php while( have_posts() ): the_post();?>
<?php if(comments_open()){ ?>
<div class="news-row">
<?php if (has_post_thumbnail( $post->ID ) ): ?>
<div class="newsimagebox">
<?php //$feat_image = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID),'thumbnail');
$images = the_post_thumbnail();?>
<?php echo $images;?>
</div>
<?php endif; ?>
<div class="news-content">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt();?></p>
<div class="readmore">Read More</div>
</div>
</div>
<?php } ?>
<?php endwhile;?>
<?php endif; //wp_reset_query(); ?>
Thanks

Categories