I am having difficulty implementing a PHP counter, to wrap my list elements in unordered list tags.
I want to wrap around every 3 list elements.
I have been trying to use these previous questions as a guide but to little avail.
easier way to get counter from while loop?
php loop counter bootstrap row
<?php
$counter = 0;
echo '<ul class="products latestCourses">';
while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php $counter = $counter++; ?>
<?php if ($counter%3 == 0) echo '</ul><ul class="products latestCourses">'; ?>
<?php endwhile; // end of the loop. ?>
This is what I have so far the page template simply contains a list element.
Currently this code is wrapping every list item in an unorder list.
<?php $counter = $counter++; ?>
this line is wrong, use either
<?php $counter++; ?>
or
<?php $counter = $counter +1; ?>
Use this for increment by 1
$counter = $counter + 1;
or
$counter = $counter + n;
where 'n' is the desired number by which you want to increment
I'm trying to make a "related" module with Wordpress and Advanced Custom Fields (ACF).
I've got the following loop:
<?php
// Settings
$categories = get_the_category();
if ( count($categories) < 2 ) {
$cat = $categories[0]->cat_ID;
} else {
$cat = $categories[1]->cat_ID;
}
$query = new WP_Query('cat='. $cat);
?>
<section class="related">
<?php // The loop
if ($query->have_posts()):
while ($query->have_posts()) : $query->the_post();
// The content part
if (have_rows('content')):
while (have_rows('content')) : the_row();
// Only print cover
if (get_row_layout() === 'cover'):
// Cover content...
endif;
// End: The content part
endwhile;
endif;
// End: The loop
endwhile;
// Reset loop, keep Wordpress from looping through unwanted modules
wp_reset_postdata();
endif;
?>
</section>
I want the loop to only print the cover module, instead it prints all the fields inside the content-module, which is a flexible content field. Am I overseeing something that causes it to loop other modules apart from cover?
Fixed it by pulling the while-loop out of // The content part
I'm trying to output a WP loop with a (class name) variable which changes depending on which iteration it is, as defined in this regex.
$mod = 0;
$rows = 0;
for ($i = 0; $i < 10; ++$i) {
$output = " ";
if ($rows % 2 == 1) {
$output = "flip";
}
// Build the post - includes the variable $output
get_template_part( 'partials/loop', 'main' );
if (++$mod % 2 == 0) {
$mod = 0;
++$rows;
}
}
But this doesn't work; instead outputting many duplicates of the same posts with no output.
I'm clearly doing something wrong; maybe approaching the problem in the wrong way.
Tip: The expression tests for every other pair such as 11 - 00 - 11 - 00
The loop, showing desired variable position:
<div class="post small-12 medium-6 columns <?php echo $output; ?>" id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>">
<h2 class="blog-title">
<?php the_title(); ?></h2>
</a>
</div> <!-- ends post -->
You can't pass a variable to your template via get_template_part(). You need to instead include your template with:
include( locate_template( 'partials/loop-main.php' ) );
// or whatever your template name is...
However, this also highly depends on where you loop is (since you haven't shown it).
For more information, see this answer on WPSE.
So in your case, you'd have something like:
<?php if ( have_posts() ): ?>
<?php // Your initial iterator/counter logic goes here... ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
// Test your counter to see if you need to add a class on this post
// include() the template, rather than get_template_part() so that we can pass the
// counter to the partial template.
include( locate_template( 'partials/loop-main.php' ) );
// Increment the counter
?>
<?php endwhile; ?>
<?php endif; wp_reset_postdata(); ?>
I am having a loop and that loop has only sticky posts in it. So my logic works like this:
"If Sticky Posts are "EMPTY" break the loop". That code works as expected and looks like this:
<?php //we will get "Sticky Posts" only with this loop and exlude Featured Category
$category = get_cat_ID('Featured');
$col = 1; //Let's create first column
$sticky = get_option( 'sticky_posts' );
$args = array(
/* Add whatever you need here - see http://codex.wordpress.org/Class_Reference/WP_Query */
'paged' => $paged,
'category__not_in' => array($category),
'post__in' => $sticky,
'ignore_sticky_posts' => 1
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query($args);
/*Below is IMPORTANT PART*/
if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(empty($sticky))break;?>
<div <?php post_class('col'.$col); ?> id="post-<?php the_ID(); ?>">
<?php if ($col == 1) echo '<div class="row">';//If column 1 create first row ?>
<?php if ($col == 2) echo '<div class="row2">';//If column 2 create second row ?>
<h3 class="mytitle"><?php the_title(); ?></h3>
<div class="entry">
<?php if ( has_post_thumbnail() ):?>
<div class="featured_img">
<?php
the_post_thumbnail();
echo '<div class="featured_caption">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
?>
</div><!--/featured_img-->
<?php endif; ?>
<?php // let's enable more link on pages...
global $more;
$more = 0;
?>
<?php the_content(__('Read more','override')); ?>
<div class="clear"></div>
<div class="custom_fields"><?php the_meta(); ?></div><br/>
<p class="postmetadata">
<?php _e('Filed under:','override'); ?> <?php the_category(', ') ?> <?php _e('by','override'); ?> <?php the_author(); ?><br/><?php the_tags(__('Tags:','override'), ', ', '<br />'); ?>
<?php _e('Posted on: ','override'); ?><?php the_time(get_option('date_format')); ?><br/>
<?php if ( comments_open() ) {
comments_popup_link(__('No Comments »','override'), __('1 Comment »','override'), __('% Comments »','override'));}
else {
_e('Comments are disabled!','override');
}
?>
<?php edit_post_link(__(' Edit','override'), __(' |','override'), ''); ?>
</p>
</div><!--/entry-->
</div><!--/post_class-->
<?php /*Enable Two Column Layout*/
if($col==1) {
$col=2;
echo "</div>";
}
else if($col==2) {
$col=1;
echo "</div>";
}
endwhile; ?>
<?php endif; ?><!--END if THE LOOP (Sticky)-->
<?php
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
Now before this working code I tried a different logic that goes like this:
"If NOT EMPTY continue the loop" so now everything in my code stays the same except:if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(empty($sticky))break;?> so now that code becomes:if($wp_query->have_posts()):?><?php while ( $wp_query->have_posts() ) :
$wp_query->the_post();if(!empty($sticky))continue;?>
Now this is where i got confused because if(!empty($sticky))continue; part does not work as expected because my loop CONTINUES (returns other posts) even if there are no "Stickies". I thought that loop will STOP if there are no stickies but it is not the case. My var_dump($sticky)
shows this if there are sticky postsarray(1) { [0]=> int(214) } and shows this if there are no stickiesarray(0) { }.
My question is: Why the loop continues to return other posts if using if(!empty($sticky))continue; (i thought it will return ONLY "Stickies" if they exist and return NOTHING if they are not here. )
Thank you!!
First off, let me poit out that your logic doesn't quite agree with your code :).
From what I understand from your code, you want to iterate all posts WP_Query() returned, but only render sticky ones. Your if is inside the wile loop, so you have to check if the current post is sticky or not. However, if(empty($sticky)) doesn't do that. It checks if there are any sticky posts at all. A way to check the current post would be if(is_sticky(the_ID())).
Now, concerning continue:
From the php manual:
continue is used within looping structures to skip the rest of the
current loop iteration and continue execution at the condition
evaluation and then the beginning of the next iteration.
So as you can see, continue doesn't stop the loop, but rather attempts to start the next iteration ignoring the rest of the code for the current step. Which is what you want, if the current post is not sticky, in other words if(!is_sticky(the_ID())).
However, I think you don't really need any check at all, since you already specified that you want WP_Query() to fetch only stickies ('post__in' => $sticky).
See also: this WordPress Answers topic.
I don't know well the wordpress code, but in that code, the $sticky variable is never updated in the while loop. So maybe you have to add $sticky = get_option( 'sticky_posts' ); right before the if condition.
I can’t figure out why this won’t work? I have more than 15 posts but they’re getting cut off the page.
The site: http://gruntsandco.com/
My loop:
<?php $latest = new WP_Query('showposts=15'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
Loopageness
<?php endwhile; ?>
You can try 'posts_per_page' attribute for WP Query.
<?php $latest = new WP_Query('posts_per_page=15'); ?>
<?php while( $latest->have_posts() ) : $latest->the_post(); ?>
Loopageness
<?php endwhile; ?>
If you want to show all posts just set this parametr to -1.