Display total number of items from while loop outside of it php - 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;

Related

How to Split list of content after a certain amount of rendered elements

I am trying to get a block to split content after so many entry's have been selected by the user in the backend of my website.
This is what is is supposed to look like
However at the moment I cannot get it to break off a second line. here is the code below to show how I am rendering my block along with an image of what is being rendered
function roomFacilitiesLists(){
$roomFacilitiesList1 = get_field ("facilities_list_1") ;
$roomFacilitiesList2 = get_field ("facilities_list_2") ;
$roomFacilitiesList3 = get_field ("facilities_list_3") ;
$roomFacilitiesLeftSideImage = get_field ("room_facilities_left_side_image") ;
$roomFacilitysBackgroundImage = get_field ("room_facilitys_background_image") ;
?>
<div class="left-side-image-block-parent">
<div class="left-sided-image">
<img src="<?php echo $roomFacilitiesLeftSideImage['url']; ?>" ; />
</div>
</div>
<div class="right-side-room-facilitys">
<div class="room-facilities-lists" style="background-image: url(<?php echo $roomFacilitysBackgroundImage['url']; ?>)"> ;
</div>
<div class="main-lists-container">
<div class="column-1-list">
<?php
if( $roomFacilitiesList1 ): ?>
<ul>
<?php foreach( $roomFacilitiesList1 as $roomFacilitiesList1 ): ?>
<div><?php echo $roomFacilitiesList1; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-3-list columns-2">
<?php
if( $roomFacilitiesList3 ): ?>
<ul>
<?php foreach( $roomFacilitiesList3 as $roomFacilitiesList3 ): ?>
<div><?php echo $roomFacilitiesList3; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
<div class="column-2-list">
<?php
if( $roomFacilitiesList2 ): ?>
<ul>
<?php foreach( $roomFacilitiesList2 as $roomFacilitiesList2 ): ?>
<div><?php echo $roomFacilitiesList2; ?></div>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</div>
</div>
Now this makes up 3 columns of data however the other 2 are not the issue here is "columns-3" which is the data output causing the issues.
I have tried using https://www.php.net/manual/en/function.str-split.php as well as https://www.php.net/manual/en/function.explode but with no prevail any help here would be great.

Reset counter after each post loop

I'm having an ACF repeater inside a custom post type loop with a counter that makes a row around every two col-md-6's. This works great when I have an even number of col's but not when it's uneven. When a post has an uneven number of col's the counter somehow remembers that for the next post and only displays one col in the first row.
Down here you'll find the current code and a little picture of what is happening.
Somehow I need to reset the counter after each post loop but can't figure it out. wp_reset_postdata doesn't seem to work.
<?php $args = array( 'post_type' => 'posttypename', 'posts_per_page' => '-1' ); $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div>
<div class="container">
<div class="row">
<div class="col-md-12">
<h1><?php the_title(); ?></h1>
</div>
</div>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Just a small change. You need to make sure that your counter is reset to 0 before the loop for the acf starts.
<?php $counter = 0; //Initialize your Counter here before the Loop Starts for each Post ?>
<?php if( have_rows('row') ): ?>
<div class="row">
<?php while( have_rows('row') ): the_row(); $text = get_sub_field('text'); ?>
<div class="col-md-6">
<?php echo $text; ?>
</div>
<?php $counter++; if($counter % 2 === 0) : echo '</div> <div class="row">'; endif; ?>
<?php endwhile; ?>
</div>
<?php endif; ?>

foreach statement within ACF repeater

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.

Advanced Custom Field doesn't show in wordpress page

I have this bit of code that doesn't render on page. Does anybody know what the issue is?
<div class="b b--alt">
<div class="container">
<div class="tab-widget tab-widget--team js-tab-widget">
<?php if( have_rows('the_commusoft_story') ) { ?>
<?php $i = 0; ?>
<ul class="tab-widget__list">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php $i++ ?>
<li class="tab-widget__item">
<a href="#tab-panel-<?php echo $i; ?>" class="tab-widget__link">
<span class="ss-icon <?php the_sub_field('icon'); ?>"></span>
<h2><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('section_description'); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php }; ?>
<?php if( have_rows('the_commusoft_story') ) { ?>
<?php $i = 0; ?>
<div class="tab-widget__tabs">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php $i++ ?>
<div class="tab-widget__tab-content">
<h2 id="tab-panel-<?php echo $i; ?>"><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('content'); ?>
</div>
<?php endwhile; ?>
</div>
<?php }; ?>
</div>
</div>
</div>
SO on the page are two sections, the problem appears when I try to move it as second section and it disappears...
Any idea what is going on, and where the mistake is?
Thanks
Your code can definately be simplified. I've removed the extra IF statement - it's not required. I've also added line terminations to the $i++ operators:
<div class="b b--alt">
<div class="container">
<div class="tab-widget tab-widget--team js-tab-widget">
<?php if( have_rows('the_commusoft_story') ) : ?>
<?php $i = 0; ?>
<ul class="tab-widget__list">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php
$i++; // This starts at 1.
?>
<li class="tab-widget__item">
<a href="#tab-panel-<?php echo $i; ?>" class="tab-widget__link">
<span class="ss-icon <?php the_sub_field('icon'); ?>"></span>
<h2><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('section_description'); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php $i = 0; ?>
<div class="tab-widget__tabs">
<?php while( have_rows('the_commusoft_story') ): the_row(); ?>
<?php
$i++;
?>
<div class="tab-widget__tab-content">
<h2 id="tab-panel-<?php echo $i; ?>"><?php the_sub_field('section_title'); ?></h2>
<?php the_sub_field('content'); ?>
</div>
<?php endwhile; ?>
</div>
<?php endif; ?>
</div>
</div>
</div>
This, in theory, should be a working piece of code. So - if what I have written doesn't work for you; I'd advise checking for PHP errors, or possibly looking for CSS styling conflicts.
Keep us posted how you get on :)

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