I am trying to figure out a way to print the sum of all the posts displayed in a loop. For example:
<?php if (have_posts()) :
$total_count = 1;
while (have_posts()) : the_post();
echo $total_count;
endwhile;
endif;
?>
Returns: 1 1 1 1 1 1 1
Since there are 7 posts in my loop. However, I would like to make a sum of all those 1's in order to get 7 as the result.
Any ideas?
Try to increment your total_count, instead of just printing it every time.
You will add 1 for each post and then print the total value. Check php assignment operators for +=
<?php if (have_posts()) :
$total_count = 0;
while (have_posts()) : the_post();
$total_count += 1;
endwhile;
echo $total_count;
endif;
?>
Also keep in mind your counter should always start at 0.
Related
I'm working on the search results output for Wordpress website and would like to split a row each third post. Here is what I have at the moment:
<?php if ( have_posts() ) : ?>
<div class="post-wrapper">
<?php
while ( have_posts() ) :
the_post();
?>
<?php get_template_part( 'partials/content', get_post_format() ); ?>
<?php endwhile; ?>
</div>
<?php else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>
Now it gets all results but just in one column and I'd like to have three columns. Theme is using Bootstrap so all I need is to add a 'row' each third post but I just can't figure out how to implement this into the PHP template as above. Can somebody help me?
Try this for inspiration. Outputs:
0
1
2
-- BREAK --
3
4
5
-- BREAK --
6
7
8
-- BREAK --
9
Code:
<?php
// Loop 10 times.
for ($i = 0; $i < 10; $i++)
{
// If $i MOD 3 evaluates to zero, then we've reached the end of a group of three items.
// Skip when $i is zero, unless you want to show a break before any output.
if ($i && !($i % 3))
// Output a row separator
echo "-- BREAK --\n";
// Output a row with some column values.
echo "$i\n";
}
Group every 3 posts in one row. Checking the total number of posts avoids a new div in the last element.
$loop_posts = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'no_found_rows' => true,
)
);
$count = 1;
$found_posts= $loop_posts->post_count;
if ($loop_posts->have_posts()) :
echo '<div class="row">';
while ( $loop_posts->have_posts() ) : $loop_posts->the_post(); ?>
<?php
get_template_part( 'includes/news_element', get_post_format() );
if($count % 3==0 && $count <= $found_posts){
echo '</div><div class="row">';
}
$count++;
?>
<?php
endwhile;
echo '</div>';
wp_reset_postdata();
endif;
I am currently trying to a create a feature on my category pages where on every 4th preview post, within the category an advert block is display.
essential it will work the following way:
post 1
post 2
post 3
post 4
ADVERT BLOCK
Post 5
Post 6
In my <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
I have the following:
<?php $i = 0; ?>
<?php if (post_num ($i%4 == 0) < (5 - $featured_count)) : echo "this works"; endif ?>
can someone guide me in the right direction
Your more or less there, here's a quick example just showing some arbitrary text or the post title:
<?php
$counter = 0;
if (have_posts()) {
while (have_posts()) {
$counter++;
the_post();
if ($counter % 5 === 0) {
echo 'Advert Here!';
} else {
the_title();
}
}
}
?>
I haven't tested this, but just to give you an idea.
Edit: I should note that since you're looking to insert the advert after the fourth post, $counter % 4 won't do what you think, if you want to show four posts and then the advert, it would be placed in the 5th "position", hence $counter % 5.
I'm currently counting the posts in the loop...
<?php $count = 0; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $count++; ?>
<?php include(locate_template('content.php')); ?>
<?php endwhile; ?>
But need to select specific posts programmatically in an if statement.
The post count I need to select is 1, 4, 5, 8, 9, 12, 13 etc (+3+1r) in sequence.
How can I select these posts (without having to manually type the numbers)?
While technically off topic for WordPress, I think this is something that a good number ueses will enquire about, particularly those that are new to both WordPress and PHP.
As suggested in the comments to your question, you can use a modulus operator to check this, and hopefully this answer will solve your problem.
<?php
$count = 0;
/* Start the Loop */
while ( have_posts() ) : the_post();
$count++;
if($count % 4 === 0 || $count % 4 === 1) :
locate_template('content.php', true);
endif;
endwhile;
?>
As a side note, the locate_template function will automatically load the template file with require_once (if you set the $load parameter), so you don't need to wrap it in include().
I'd recommended that you check if the template exists, and if not fall back on a theme default that will always be there.
I have a query that gets the 5 highest rated content on my blog how can i make it so for each post that is shown with the query 1 is added to the a $number variable?
Basically what i want is to give the highest rated content a number between 1-5 the first gets 1 the second get 2, third get 3 and so on.
<?php ( query_posts('category_name=music&showposts=3&orderby=comment_count&order=desc&post_status=future,publish') ) ; ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Try this - just set $number to 0 and then use ++ to increment the number,
<?php ( query_posts('category_name=music&showposts=3&orderby=comment_count&order=desc&post_status=future,publish') ) ; ?>
<?php $number=0; while ( have_posts() ) : the_post(); $number++; ?>
Rated #<?= $number ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
Problem.
What I am trying to do is to call a category specific loop, however, I want whats returned to display from most recent first, to be numerically numbered so that for every 2 that is displayed, will echo a css class ascertained to them and the 3rd result to display a completely different class as this is how I have written my html. Here is what I am trying to get the HTML to display:
<div id="content">
<div class="block1"></div>
<div class="block1"></div>
<div class="block2"></div>
<div class="block1"></div>
<div class="block1"></div>
<div class="block2"></div>
</div>
If there are more results then the first two will be named in the first div and the 3rd of all the results will have that class name assigned to it.
Help would be more than greatly appreciated.
Remarked:
<?php query_posts( 'cat=featured&showposts=4' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php foreach($recent as $index => $postObj) {
$class = $index + 1 % 3 === 0 ? 'block2' : 'block1';
}
?>
<h1><?php the_title(); ?></h1>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php get_footer(); ?>
however its returning the number of posts but under the posts its returning
Warning: Invalid argument supplied for foreach()
Have tried trial and error however, I think that my grammar is atrocious.
What you're looking for is the modulo operator. What modulo does is find the remainder of a division operation. In effect the result is in a range of 0..N-1, where N % N = 0.
foreach($posts as $index => $postObj) {
$class = $index + 1 % 3 === 0 ? 'block2' : 'block1';
This accomplishes what you want because the loop logic looks like:
1 % 3 = 1 -> block1
2 % 3 = 2 -> block1
3 % 3 = 0 -> block2
Your code needs to be:
<?php
query_posts( 'cat=featured&showposts=4' );
$index = 1;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$class = $index++ % 3 === 0 ? 'block2' : 'block1';
?>
<div class="<?php echo $class ?>">
<h1><?php the_title(); ?></h1>
</div>
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
<?php get_footer(); ?>
The $index++ operator means, "increment $index after this use of it." So, notice how the loop is set up. Before the loop we set $index to 1. Inside the loop we set $class using our modulo technique, then increment $index. Then we have to create a container DIV, like you mentioned, and echo the class there.