PHP counter increment for every 1st and 4th div - php

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).

Related

Unset variable at the end of if else statement

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";

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.

While loop, wrap items in a count in a separate div (index.php)

I have the following so far:
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<?php the_title();?>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<?php the_title();?> blah
<?php the_excerpt();?>
<?php endif; ?>
<?php endwhile; ?>
I want to wrap the first 4 in a '<div class="top"></div>' and the bottom 6, I want all of them wrapped in a '<div class="bottom"></div>'
Any tips of how this can be done using the while loop would be greatly appreciated
Try the below snippet.
<?php
$count = 0;
while (have_posts()) : the_post(); $count++;
if ( $count == 1 ) echo '<div class="top">';
if ( $count > 5 ) echo '</div><div class="bottom">';
the_title();
if ( $count > 5 ) the_excerpt();
endwhile;
echo "</div>"
?>
This might help you.
<?php $count = 0; while (have_posts()) : the_post(); $count++; ?>
<?php if ($count < 5) : ?>
//four posts here, all them wrapped in one div, these will also have different markup
<div class="top">
<?php the_title();?>
</div>
<?php else : ?>
//remaining six posts here, all of them wrapped in one div, these will also have different markup
<div class="bottom">
<?php the_title();?> blah
<?php the_excerpt();?>
</div>
<?php endif; ?>
<?php endwhile; ?>

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; ?>

Can't get this overly complicated loop to work correctly

So I'm editing the index.php in my WP theme's folder, essentially what I want it to do is this:
Show 4 Posts on first page of blog.
Style the first (newest) post on the first page differently. Style the other 3 the same.
Show 6 Posts on every other paginated page.
Here's the loop I'm working with, the first page appears fine, but for some reason on page 2 and higher it's executing really weird and showing only one post on each additional page. Also, it will only show the title and not the date or excerpt. Here's my code:
<?php
if( is_home() && !is_paged() ){
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 4;
query_posts($args);
if (have_posts()) :
while (have_posts()) : the_post();
if (++$counter == 1) { ?>
<div class="featured_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail('featuredblog'); ?>
<?php the_excerpt(); ?>
</div>
<?php } else { ?>
<div class="regular_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<div class="postimage"><?php the_post_thumbnail('regularblog'); ?></div>
<a href="<?php the_permalink(); ?>"><h3><?php
// short_title($after, $length)
echo short_title('...', 7);
?></h3></a>
<?php the_excerpt(); ?>
</div>
<?php } ?>
<?php endwhile;
else :
// Code for no posts found here
endif;
} else {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 6;
query_posts($args); ?>
<div class="regular_post">
<p class="date"><?php the_date('M j, Y'); ?></p>
<div class="postimage"><?php the_post_thumbnail('regularblog'); ?></div>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); ?>
</div>
<?php } ?>
Maybe there's too many if...else statements in there?
Are you using some kind of framework? that's a pretty bad tempalte engine.
Anyway if yuo need to add style, you should leave the job to CSS.
if you need extremly precision you can just leave a class with a counter for each element you need to style differently.
like <div class="Element<?php echo ++$counter; ?>"></div>
and then you can add your styles in CSS
.Element1,.Element2,.Element3 {styles}
.Element4 {other styles}
in this way you simplify the life with your php code
Keep in mind one day you will not even need to add a class counter.. In the near future you could use the nth-child css selector http://reference.sitepoint.com/css/pseudoclass-nthchild
Edit: damn me I replied someone with 50% AR
my suggestion would follow yes123, but really leave it all to css. basically, all of your posts should be wrapped in a parent div, and each should be classed the same way.
<div id="content">
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
<div class="post">
<div class="title"></div>
</div>
</div>
that's about what you should resolve to, with more tags in the post, obviously.
from there, the way to style the first post differently would be using the #content > .post:first-child selector.
also, having a different number of posts per page is probably not a good idea. i've actually never really tried it, but i can see where the paging would get screwed up, and you'd probably never see posts 5 or 6. i'm thinking wordpress would probably say that with 6 posts per page, page 2 should always start with post 7. not positive though.
<?php if (is_home() && !is_paged() ) {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 4;
query_posts($args);
} else {
global $query_string;
parse_str( $query_string, $args );
$args['posts_per_page'] = 6;
query_posts($args);
} ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="post<?php echo ++$counter; ?>">
<?php if ( is_home() && !is_paged() && $counter == 1 ) { ?>
do stuff
<?php } else { ?>
do other stuff
<?php } ?>
</div>
<?php endwhile; ?>

Categories