Limit wordpress loop to 10 or 20 posts - php

I am using the custom field plugin and i have made some custom fields in wordpress.
I wrote php to bring to the front the custom fields using the following code, but it doesn't stop looping and I need only 10 or 20 times to loop.
So I want to change but I don't know how.
<?php if( $banner_query2->have_posts() ): ?>
<?php while ( $banner_query2->have_posts() ) : $banner_query2->the_post(); ?>
<?php
$post_id = get_the_ID();
?>
<?php
$featured_matches = get_field('locations');
if( $featured_matches ): ?>
<ul>
<?php foreach( $featured_matches as $post ):
// Setup this post for WP functions (variable must be named $post).
setup_postdata($post); ?>
<!-- <?php //the_title(); ?> -->
<span><?php the_field( 'customfile' ); ?></span>
<span><?php the_field( 'customfile2' ); ?></span>
<?php endforeach; ?>
</ul>
<?php
// Reset the global post object so that the rest of the page works correctly.
wp_reset_postdata(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>

Its quite possible to terminate a foreach loop or any other early, by using break; so all you need to do is maintain a counter and when it reaches the magic number break the loop.
<?php
$cnt = 0;
$maxcnt = 10;
foreach( $featured_matches as $post ):
// do your funky stuff
$cnt++;
if ( $cnt >= $maxcnt ) :
break;
endif;
endforeach;

Seems like in wordpress if you add just break; in the end of the foreach loop works fine

Related

shortcode to display nested ACF repeater fields

In Wordpress, in order to display a nested repeater ACF field, I adapted a few lines in php that I insert in the archive page of post.
In the archive, the hard code runs very well and the lines created by the code are displayed as expected.
I need to use this code via Elementor. To do that, I need to create a shortcode.
I created a shortcode with the code.
Nothing runs good, nothing is displayed, even when I desactive Elementor
Can you, please, help me,
Bruno
Here is the code:
function acf2repeaters_ingredients_function() {
while ( have_posts() ) : the_post();
// check for rows (parent repeater)
if( have_rows('en-tete_ingredients') ): ?>
<div id="en-tete_ingredients">
<?php // loop through rows (parent repeater)
while( have_rows('ingredients') ): the_row(); ?>
<div>
<h3><?php the_sub_field('en-tete_ingredients'); ?></h3>
<?php // check for rows (sub repeater)
if( have_rows('liste_des_ingredients') ): ?>
<ul>
<?php // loop through rows (sub repeater)
while( have_rows('ingredients') ): the_row(); ?>
<li <?php echo ' class="nombre"'; ?>><?php the_sub_field('quantite');?></li>
<li <?php echo ' class="unite"'; ?>><?php the_sub_field('unite');?></li>
<li <?php echo ' class="ingredient"'; ?>><?php the_sub_field('ingredient');?></li>
<?php endwhile; ?>
</ul>
<?php endif; //if( get_sub_field('ingredients') ): ?>
</div>
<?php endwhile; // while( has_sub_field('en-tete_ingredients') ): ?>
</div>
<?php endif; // if( get_field('en-tete_ingredients') )
endwhile; // end of the loop.
}
add_shortcode( 'acf2repeaters_ingredients', 'acf2repeaters_ingredients_function' );
Solution:
Remove line: while ( have_posts() ) : the_post();
Remove line: endwhile; // end of the loop.
Then the shortcode rocks!

ACF - page selector get custom field as well

I am using Advanced Custom Fields and have chosen 'Page Selector' that allows me to choose pages.
On the front end, I am able to code it so that the pages I've chosen are on the home page with the title and page link but I'm also trying to pull through a custom field in that page as well.
Here is that code I have so far;
<?php if( have_rows('page_selector') ):
$i=1;
$count = (count($my_fields['value']));
?>
<?php while( have_rows('page_selector') ): the_row();
// vars
$page = get_sub_field('page');
?>
<div class="lg-col-6 md-col-6">
<div class="sub_service">
<?php
// vars
$post_id = get_sub_field('page', false, false);
// check
if( $post_id ): ?>
<h2><?php echo get_the_title($post_id); ?></h2>
<?php endif; ?>
//This is the custom field
<?php get_sub_field('description'); ?>
</div>
</div>
<?php
$i++;
endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
Apologies in advance if it doesn't make sense, will try and explain further if it's confusing.
Thanks
You post the question, then the answer comes to you...
<?php the_field('description', $post_id); ?>

Query Advanced Custom Field not displaying

We have ACF Pro for WP and we have created an ACF which show a location which is a select.
When trying to output we are getting this:
Notice: Trying to get property of non-object in
/home/cwplantactiveint/public_html/wp-content/themes/cwplant/loop-jobs.php
on line 66
Which is this
<?php $location = get_field('job_location'); echo $location->post_title; ?>
Now oddly, it outputs another custom field which was createdto output the date:
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
The whole code block looks like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php /* Check closing date is not past. */
$today = strtotime("now");
$closedate = strtotime(get_field('closing_date'));
if ($today < $closedate || !get_field('closing_date')) {
?>
<div class="singlepost infobox info-job content cfix">
<h2><?php the_title(); ?></h2>
<p><span class="red first">Location:</span> <?php $location = get_field('job_location'); echo $location->post_title; ?>
<span class="red">Closing Date:</span>
<?php if(get_field('closing_date')) { ?>
<?php the_field('closing_date'); ?>
<?php } else { ?>
Ongoing
<?php } ?>
</p>
<?php if ( is_archive() || is_search() || is_home()) : // Only display excerpts for archives and search. ?>
<?php the_excerpt(); ?>
<a class="button" href="<?php the_permalink(); ?>">View Details</a>
<?php else : ?>
<?php the_content( __( 'Continue reading →', 'twentyten' ) ); ?>
<?php endif; ?>
</div>
<?php $jobstrue = 'true'; ?>
<?php } else { ?>
<?php $jobsfalse = 'true'; ?>
<?php } ?>
<?php endwhile; // End the loop. Whew. ?>
I think your problem is that you haven't reset the $post object with wp_reset_postdata() so your query is returning the last thing in $post (likely createdto in your case).
Whenever I handle any object in WP, I always set it up and then reset it like this:
<?php
// YOUR CUSTOM FIELD
// 0- Reset post object from last query (this should really be part of your last query)
wp_reset_postdata();
// 1- Put custom field into post object and check for content
$post_object = get_field('location');
// 2- Check to make sure object exists and setup $post (must use $post variable name)
if ($post_object) {
$post = $post_object;
setup_postdata($post);
// 3- Do some magic
echo get_field('closing_date');
// 4- reset postdata so other parts of the page can use it
wp_reset_postdata();
}
?>

Wordpress Custom Query Posts Showposts number

Below is a script that I'm still unsure on how to get it to work, in Wordpress I have a repeater field that I can input the number of days that are in a month, so it creates calendar squares for me to highlight in a booking process.
What I want to do, is to have the field 'how_many_days' to run a loop that will then repeat the number of divs calendarPost. So Ideally I can input separate number of loops.
A live version of the output is here: http://universitycompare.com/school-bookings/
<?php if(get_field('calendar_repeater_field')): ?>
<?php while(has_sub_field('calendar_repeater_field')): ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php $wp_query->query('showposts='.$numberoffields ); if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php endwhile; endif; wp_reset_query(); ?>
<?php endwhile; ?>
<?php endif; ?>
FYI - I didn't know whether this would be a PHP related problem or WP only, so please advise if it this post should be elsewhere and I will remove and repost in the correct stackoverflow forum.
Your question didn't completly explain if you were in fact trying to output posts so below is a couple of suggesstions.
I'll start with what I think you're trying to do:
If you're' just wanting to output the div.calendarPost over and over (based on the number of days) then you don't need a WordPress loop for that. A standard PHP for loop will do
<?php if ( get_field('calendar_repeater_field' ) ) : ?>
<?php while ( has_sub_field('calendar_repeater_field' ) ) : ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php for ( $i=0; $i < $numberoffields; $i++ ) { ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php } ?>
<?php endwhile; ?>
<?php endif; ?>
If however you're wanting to output posts (based on the number of days in the ACF field) then you would use the below code.
<?php if ( get_field('calendar_repeater_field' ) ) : ?>
<?php while ( has_sub_field('calendar_repeater_field' ) ) : ?>
<?php $numberoffields = get_sub_field('how_many_days'); ?>
<?php $calendar_posts = new WP_Query('posts_per_page=' . $numberoffields); ?>
<?php if ( $calendar_posts->have_posts() ) : while ( $calendar_posts->have_posts() ) : $calendar_posts->the_post(); ?>
<div class="calendarPost">
<span class="title"><?php the_sub_field('calendar_month_name'); ?><span class="circleOpen"></span></span>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
<?php endwhile; ?>
<?php endif; ?>
Refer to "The Usage" section of the WP Codex for more info: http://codex.wordpress.org/Class_Reference/WP_Query.
Hope that helps.

highlighting current post in recent posts sidebar , wordpress

Im just wondering how I could get the post id of the blog post I am currently reading while in another loop (i.e. the recent posts loop in the sidebar)
In my single post php file I have this code creating a variable
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'single' ); ?>
<?php $current_post_id = get_the_ID(); ?>
<?php endwhile; // end of the loop. ?>
Then in my default-widgets php file, in the loop for recent posts I have this...
<?php while ($r->have_posts()) : $r->the_post(); ?>
<?php $recent_post_id = get_the_ID(); ?>
<li> <?php echo $recent_post_id; if ( $recent_post_id == $current_post_id ) { echo 'pass '; } else { echo 'fail ';} ?></li>
<?php endwhile; ?>
Its giving fail every time, so obviously what Im doing doesnt make sense (Im still learning). Im just wondering is there a way to grab the post id from the first loop, and use it in the second. At the moment when I echo current_post_id in the second loop nothing shows up.
(Regarding highlighting the current post, that'll be easy once I get this working.
Thanks for the help.
There are a lot of examples online on how to get the ID outside the loop.
try this in the widget code:
<?php
global $post;
$current_post_id = $post->ID;
while ($r->have_posts()) : $r->the_post(); ?>
<li> <?php echo $recent_post_id; if ( $recent_post_id == $current_post_id ) { echo 'pass '; } else { echo 'fail ';} ?><?php if ( get_the_title() ) the_title(); else the_ID(); ?></li>
<?php endwhile; ?>
Let us know if it works.

Categories