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(); ?>
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 use a wordpress plugin in which i can add custom data for each category
Now because of some error it doesn't always grab the correct data. to fix that i wrote the following:
<?php if ( in_category( 3 ) ) :
?>
<?php the_field( "categoryselect", "category_3" ); ?>
<?php endif; ?>
Now that works, but is there a way to loop through all my categories with this?
(basicly i need the "3" and the number in "category_3" to loop through all my categories)
i couldnt get it to work, so was going to hard code it, but thought i would ask for help before hardcoding it :P
Thnx in advance
Edit, with help from Ketan, i came to this solution that works for my problem
<?php
$category = get_queried_object();
$testtest = $category->term_id;
if ( in_category( $testtest ) ) :
?>
<?php the_field( "categoryselect", "category_".$testtest ); ?>
<?php endif; ?>
You can do something like this:
<?php
for($i = 0; $i < categories_count; $i++) {
if ( in_category( $i ) ) :
the_field( "categoryselect", "category_".$i );
endif;
}
?>
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.
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.
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.