Unset variable at the end of if else statement - php

I am using 2 if else statements where the first set just displays 4 results, and the second should display all results (including the initial 4)
I have got the counter working but it also effects the second set of results (which only displays the remaining results minus the first 4) and I can't work out how to unset the variable.
I have tried using 'unset' in different places, also tried setting a new rule for the second batch but nothing works. Unset works at different places but also unsets the initial 4, thereby displaying all on both.
Any help would be greatly appreciated
<section>
<div class="full-width-inner">
<div class="find-anything-mobile-grid">
<?php if ( have_rows( 'carousel_search_item' ) ) : ?>
<?php $i = 0; ?>
<?php while ( have_rows( 'carousel_search_item' ) ) : the_row(); ?>
<?php $i++; ?>
<?php if( $i > 4 ): ?>
<?php break; ?>
<?php endif; ?>
<div class="lenses-carousel-slide">
<p>content</p>
</div>
<?php unset($i); ?>
<?php endwhile; ?>
<?php else : ?>
<?php // No rows found ?>
<?php endif; ?>
</div>
<div class="find-anything-mobile-grid-popup">
<?php if ( have_rows( 'carousel_search_item' ) ) : ?>
<?php while ( have_rows( 'carousel_search_item' ) ) : the_row(); ?>
<div class="lenses-carousel-popup-item">
<p>content</p>
</div>
<?php endwhile; ?>
<?php else : ?>
<?php // No rows found ?>
<?php endif; ?>
</div>
</div>
</section>

$i has no effect on the rows that are printed with the_row().
To go back to the first row, use the undocumented function reset_rows('carousel_search_item') between the two loops.

This is mostly comment, but space there is limited.
Your code is virtually unreadable. Please go and have a look at some PHP style guides. If your code contains ?>[whitespace]<?php then its wrong. Removing that is slightly better but its still confused. Why do you have empty 'else' blocks?
Cleaning this up, makes some of the bugs more obvious (to me at least):
<?php
if ( have_rows( 'carousel_search_item' ) ) :
$i = 0;
while ( have_rows( 'carousel_search_item' ) ) :
the_row();
$i++;
if( $i > 4 ):
break;
endif; ?>
<div class="lenses-carousel-slide">
<p>content</p>
</div>
<?php
unset($i);
endwhile; ?>
endif; ?>
</div>
<div class="find-anything-mobile-grid-popup">
<?php
if ( have_rows( 'carousel_search_item' ) ) :
while ( have_rows( 'carousel_search_item' ) ) :
the_row(); ?>
<div class="lenses-carousel-popup-item">
<p>content</p>
</div>
<?php
endwhile;
endif;
?>
</div>
</div>
(but stackoverflow's syntax highlighter can't make sense of this)
We don't know what "have_rows()" and "the_row()" are doing - but it smells like they are operating on the same data which is not being passed as a parameter. That's BAD.
You have 2 exits from your loop defined in different places. That;s BAD.
You are using $i as way of limiting the loop - but unsetting this variable in the middle of the loop. That's BAD.
second set of results (which only displays the remaining results minus the first 4
Unless you are doing something really, REALLY bad with scope then you've got malleable state inside "the_row()" - which is also BAD.
Move your data pointer outside of your functions.
Given the description of what you are trying to achieve, I would have written something like:
$spacer='';
for ($i=0; $i<4 && $i<count($data); $i++) {
print $spacer;
output($data[$i]);
$spacer="<div class=\"lenses-carousel-slide\">\n<p>content</p>\n</div>\n";
}
print "<div class=\"find-anything-mobile-grid-popup\">\n";
foreach ($data as $d) {
print $d . "\n";
print $spacer;
}
print "</div>\n";

Related

I want to have 3 boxes across page with one specific post in each

I am trying to have 3 boxes across my WP page.
<div class="row text-center">
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="col-md-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php static $count = 0;
if ($count == "1") { break; }
else { ?>
<div class="post">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php $count++; } ?>
<?php endwhile; ?>
<?php endif; ?>
</div>
</div>
The object is to have one different specific post in each of the three boxes.
I am trying to make a page that has every block of text easily editable from the admin rather than the code. I am using a plugin that allows me to add a shortcode to a post where I want my specific post to show.
I have the boxes in place and have everything correct.
The first box populates with the correct post as it should but the other two won't display their posts.
It seems that maybe I can only use the loop once on the page, if so how can I achieve my aims?
Thanks for reading, any understandable advice will be very welcome, I have only recently started custom themes and PhP is a bit of a struggle too so I would be grateful if the help could be dumbed down a bit.
NB: I have tried replacing break with continue as suggested but the second and third post still don't show. Is it possible that there is more than just replacing the word break with continue?
Your loop breaks out of the foreach loop on the second iteration since $count = 1. You might have wanted to do continue to skip the 2nd post?
(Don't be tempted to think that break just breaks out of the conditional block. The conditional logic simply chooses which path or block of code to execute.)
EDIT
First of all, you are doing a loop for each box...The purpose of the loop is so that you don't need to repeat your code.
Second, you are restarting the loop each time..
Use the loop like this:
$query = new WP_Query( array( 'post_type' => 'post' ) );
<?php if ( $query->have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post">
<?php the_title();
the_content();
?>
</div>
<?php endwhile;
wp_reset_postdata();
endif;
?>
There a couple ways to do the query. I do like the get_posts better for simple queries because you don't need to reset the postdata using that way. You'd pass in the same arguments to the WP_Query.
Also I recommend you create a page template for this and assign it to a page. This way if you want to change your template you can just assign the page a different one instead of modifying your index.php code.

Wrapping WordPress Posts After X Amount of Times, Without Modifying Loop?

I have a WordPress starter theme and one of the features is the ability to chose different archive formats by selecting different template parts. My index.php essentially looks like this:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!-- To see additional archive styles, visit the /parts directory -->
<?php get_template_part( 'parts/loop', 'archive' ); ?>
<?php endwhile; ?>
<?php joints_page_navi(); ?>
<?php else : ?>
<?php get_template_part( 'parts/content', 'missing' ); ?>
<?php endif; ?>
One of the archive formats is a grid format, which essentially needs to output like so:
Start Row
Post 1
Post 2
Post 3
End Row
Start Row
Post 4
Post 5
Post 6
End Row
.....
Normally, I use this method:
<?php foreach (array_chunk($posts, 2, true) as $posts) : ?>
<div class="row">
<?php foreach( $posts as $post ) : setup_postdata($post); ?>
<div class="six columns">
<?php the_content(); ?>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
However, that piece of code requires a different type of loop than the standard WP loop, which makes it hard to integrate into the theme without the user having to also make adjustments to the loop.
So my question is, is it possible to wrap X amount of posts with a div without modifying the standard WordPress loop?
You can start iterating (counting) over posts by using the iterator $i;
In your archive.php or index.php (the file which has the main query):
<?php if (have_posts()) :
$i = 1; //Start counting
while (have_posts()) : the_post(); ?>
<!-- To see additional archive styles, visit the /parts directory -->
<?php get_template_part( 'parts/loop', 'archive' ); ?>
<?php $i++; //Increase $i ?>
<?php endwhile; ?>
<?php joints_page_navi(); ?>
<?php else : ?>
<?php get_template_part( 'parts/content', 'missing' ); ?>
<?php endif; ?>
And in your parts/loop file (which has the loop "<article></article>"), make calculations to check the current post index and decide whether to skip, start or close the wrapper tag:
<?php
//Doing some math
$x = 4 //Change this to any number you want
if ( $i == 1 || $i == $x || $i % $x == 1 ) {
$before_article = '<div class="wrapper">'; //The wrapper start tag
}
if ( $i % $x == 0 || $wp_query->current_post + 1 == $wp_query->post_count ) {
$after_article = '</div>'; //The wrapper end tag
}
<?php echo $before_article; ?>
<article>
<!-- post content here -->
</article>
<?php echo $after_article; ?>
<?php $before_article = ''; $after_article = ''; ?>

ACF Repeater Fields different counts

I have a slice of code:
<?php
if( have_rows('our_people') ):
$i = 0;
while ( have_rows('our_people') ) : the_row(); ?>
<?php $i++; ?>
<?php if( $i > 3 ):
break; ?>
<?php endif; ?>
<a class="people-thumb fancybox-inline" href="#fancybox-<?php echo $i;?>">
<div class="people-thumb-image" style="width:172px;height:172px;overflow:hidden;background:transparent url(<?php the_sub_field('people_image'); ?>) no-repeat center center; background-size:cover;"></div>
<div class="people-thumb-details">
<h3><?php the_sub_field('people_name_acf'); ?></h3>
<p><?php the_sub_field('people_title'); ?></p>
</div>
</a>
<div class="peopleoverlay">
<div id="fancybox-<?php echo $i;?>">
<img src="<?php the_sub_field('people_image'); ?>" width="172" style="float:left;margin:0 10px 10px 0;"> <?php the_sub_field('people_details'); ?>
</div>
</div>
<?php endwhile;
else :
endif;
?>
What this does it simply count the entries and then stop after 3 - works fine.
However, with the other rows in the repeater field (after 3) how do I get those to also display? Reasoning is these 3 are in a seperate styling div, and all other entries are placed in another div.
However, if I try to repeat this code, nothing shows. So I must need to reset this somehow? I want all repeater rows after 3 to show in a different section.
It looks that your code specifically says not to display more than 3 results! Annotated below.
$i = 0; // Start a counter
while ( have_rows('our_people') ) : the_row(); ?> // As long as there are posts to display
<?php $i++; ?> // increment the counter
<?php if( $i > 3 ): // if the counter is 4 or more...
break; ?> // don't execute the code below; jump out of the while loop.
<?php endif; ?>
That being said, remove $i = 0, and the following lines:
<?php $i++; ?>
<?php if( $i > 3 ):
break; ?>
<?php endif; ?>
This will allow ACF to display all the people stored in that metafield.
However, if you'd like to add a different class to the 4th div and beyond, change the conditional, to remove the break statement, and to wrap the result in a div that identifies it.
<?php $i++; ?>
<?php if( $i > 3 ): ?>
<div class="other-style">
<?php endif; ?>
Then, you'd have to close it at the end with another check.
...
<?php if( $i > 3 ): ?>
</div> <!-- .other-style -->
<?php endif; ?>

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.

PHP counter increment for every 1st and 4th div

I am running a wordpress if while statement and would like my items to display in a column format.
I am currently using 960.gs which is your standard 960 grid system, and by default adds 10px padding to left and right, by simply passing a class of alpha or omega you can get rid of these.
How do I get php to execute a statment for every 1st one to add alpha and 4th one omega?
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php endwhile; endif; ?>
Try maybe something like this. I wasn't sure what you meant by every 1st and 4th so I made it the way you can see. You should be able to customize it yourself.
<?php $counter = 0; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php
$div = '<div class="entry';
if ($counter % 4 == 0)
$div .= " alpha";
else if($counter % 4 == 3)
$div .= " omega";
echo $div . '">';
?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php $counter++; ?>
<?php endwhile; endif; ?>
Try this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="grid_4">
<?php $wp_query->is_home = false; ?>
<div class="entry <?php if (!($i++ % 4)) { ?>alpha<?php } ?> <?php if (!(($j++)+1 % 4)) { ?>omega<?php } ?>">
<h3 style="margin-bottom:10px;"><?php the_title(); ?></h3>
<?php //the_excerpt() ?>
</div>
</div>
<?php endwhile; endif; ?>
Looks kind of like a repeat of something I've seen recently. (Not your fault, it was poorly named and you wouldn't have been able to search on it). Help needed improving a foreach loop
I think what you're looking for would be more precisely described as class=alpha on row 4k+1, class=omega on row=4k
I'm not familiar with your if/while syntax, and the embedded PHP throws me off a little. But I think what you want to do:
is initialize a counter to 0 before the while loop ($i = 0). Increment it at the end of the loop ($i++)
I'm not sure where you want to put these classes, but put if ($i%4==1) class="alpha" and if(i%4==0) class="omega" (add php tags as appropriate, for some reason they werent showing up right for me).

Categories