I want display wordpress loop but I want only show count of items in the loop filtered by a custom field value. I don't want to show the title or the content, only the count. This is what I have:
<?php query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php $count = 0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; //increment the variable by 1 each time the loop executes ?>
<div>
<?php if ($count==1) {
echo "1";
}
elseif ($count==2) {
echo "2";
}
elseif ($count==3) {
echo "3";
}
elseif ($count==4) {
echo "4";
} ?>
</div>
<?php endwhile; endif; ?>
but the output is "1 2" - he shows each item and gives it a number but i only want to show the count of ALL (in this case 2 ... ) items that fit to my meta_value. that means i only want to show the "2".
if you want the total count you can get it like this
<?php query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php $count = 0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; //increment the variable by 1 each time the loop executes ?>
<?php endwhile; endif; ?>
<?php echo "Count".$count; ?>
You dont need a loop to get the total number, simply use count on the posts array returned by get_posts:
echo count(get_posts(array('meta_key' => 'city', 'meta_value' => 'Seelbach')));
Edit as per comment:
$count = count(get_posts(array('meta_key' => 'city', 'meta_value' => 'Seelbach')));
if($count){
//your link code here
}
If you want to get the total number of posts inside the loop, do something like this (result will always be 2):
<?php $posts = query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
<div>
<?php echo count($posts); ?>
</div>
<?php endwhile; endif; ?>
If you need the posts count only once, place the div outside the loop
<?php $posts = query_posts('meta_key=city&meta_value=Seelbach'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
// do something with post data
?>
<?php endwhile; ?>
<div>
<?php echo count($posts); ?>
</div>
<?php endif; ?>
Related
In Wordpress, I want to have a div wrapping around every three posts (because the posts are in a grid, three per line, and I want each line to have a uniform height so the "read more" buttons line up at the bottom - http://restartcomputer.com/category/products/mac-products/). I figured out (logically) how to do this - it is basically outlined in the accepted answer of this question: PHP loop: Add a div around every three items syntax
However, I've tried everything and cannot get the code to work. The divs do not get added, at all. Here is the code:
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
Any idea why?
You have not entered back into Script (Line 4):
if (have_posts()) :
$counter = 1; ?>
<div class="entries-wrapper">
<?php while (have_posts()) : the_post(); ?>
//post stuff
<?php if ($counter % 3 == 0) { ?>
</div><div class="entries-wrapper">
<?php }
$counter += 1; ?>
<?php endwhile; ?>
</div>
//some more code
<?php endif; wp_reset_query(); ?>
I did something similar in my theme to align posts side by side:
<?php if ( have_posts() ) : ?>
<?php $col = 1; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if($col == 1) echo "<div class='row'>"; ?>
<div class="post col<?php echo $col; ?>" id="post-<?php echo the_ID(); ?>">
<?php
/* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'content', get_post_format() );
?>
<?php (($col==1)?$col=2:$col=1); ?>
</div>
<?php if($col == 1) echo "</div>"; ?>
<?php endwhile; ?>
<?php kubrick_paging_nav(); ?>
<?php else : ?>
Can also try moving the DIV within the WHILE statement.
<?php $counter = 3; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ( in_category('3') ): ?>
<?php else: ?>
<?php endif; ?>
<?php if($counter%2 == 0){echo 'floatRight';} else { echo 'floatLeft'; } ?>
<?php the_ID(); ?>
<h1 > <?php the_title(); ?></h1>
<?php the_post_thumbnail('full'); ?>
<?php the_content(__('(more...)')); ?>
<?php comments_template(); // Get wp-comments.php template ?>
<?php if($counter%2 == 0){ echo "<div class='clear'></div>";} ?>
<?php $counter++; ?>
<?php endwhile; else: ?>
<?php endif; ?>
i am trying to display in this way and i want to display particular category of post
post1
post2
post3
post4
please give me the solution ...
use the below code.
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) == 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
<?php $i = 0; rewind_posts(); ?>
<?php while(have_posts()) : ?>
<?php $i++; if(($i % 2) !== 0) : $wp_query->next_post(); else : the_post(); ?>
<?php the_content(); ?>
<?php endif; endwhile; ?>
You can use this css3 property to do it (This would take care odd and even elements, you don't have to write down the loop explicitly):
p:nth-child(odd) //you can do the same for div
{
float:left;
}
p:nth-child(even)
{
float:right;
}
You can use query_post() or Wp_query() in wordpress .
Use this code. to get the particular categories post .
<?php
// The Query
query_posts( 'cat=3' );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
?>
I'm using this code to show my 10 homepage posts:
<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
I would like somehow to identify the first post and change the code only for the first post,
For example first post should show me the_excerpt instead of the_title like this:
<?php query_posts('category_name=homepage&showposts=10&order=DESC');?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_excerpt(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
Use a variable to count the number of posts you're looping through. If the counter is 0, you're on the first post:
<?
query_posts('category_name=homepage&showposts=10&order=DESC');
$i = 0;
while ( have_posts() ) : the_post();
$i == 0 ? the_excerpt() : the_title();
the_content();
$i++;
endwhile;
?>
You can set a counter that increments each loop.
Start it by 1
$count = 1;
each loop do $count++;
if ($count ==1){the_excerpt();} else{the_content();}
My client is looking to change her homepage to look similar to this site: http://www.eatliverun.com/
Where the most recent post is up top in full in one column and the others are in two column excerpts.
She wants to have just one recent post display and the rest in the two column format. I'm just not sure how to accomplish this correctly.
Her website is located at http://pamelastable.com/
Integrating a counter into your loop can easily help you achieve this layout. Try something as follows (note: the 'six columns' class represents your 2 column layout in a 12 column grid):
<?php $count = 1; ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php if ($count == 1) { ?>
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
<?php $count++; ?>
<?php } else { ?>
<?php if($count % 2 == 0) { ?>
<div>
<? } ?>
<div class="six columns">
<article>
<div>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</article>
</div>
<?php if($count % 2 == 1) { ?>
</div>
<? } ?>
<?php $count++; ?>
<? } ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
Just add a counter to the loop and style the html differently for the first iteration.
<?php $i = 0; while ( have_posts() ) : the_post(); //inside loop ?>
<?php if ($i++ == 0) : ?>
<div>First Post</div>
<?php else: ?>
<div>Other Posts</div>
<?php endif; ?>
<?php endwhile; ?>
In your index.php or other template file^
Just use a counter to see which post number you are on.
<?php
if (have_posts()) {
$i = 0;
while (have_posts()) {
the_post();
if (++$i == 1) {
echo '<div class="entry-large">';
} else {
echo '<div class="entry-small">';
}
the_content();
echo '</div>'
}
}
I want to put in and ad code after every 6 post on my blog. I cant really figure out how to break out of the foreach and insert the ad code.
This link will help you. Third title says: Insert Ads After The First Post
Change the code for 6 where it says 2:
<?php if (have_posts()) : ?> // Here we check if there are posts
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?> // While we have posts, add 1 to count
<?php if ($count == 6) : ?> // If this is post 6
//Paste your ad code here
<h2><?php the_title(); ?></h2> // post title
<?php the_excerpt(); ?> // You may use the_content too
<?php else : ?> // If this is not post 6
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
UPDATE: As Gordon noticed, you asked for code every 6 posts (sorry I missed that on my first read). So the code should be:
<?php if ($count % 6 == 0) : ?>
As what #Gordon commented, here's how I'd re-factor that code;
<?php if (have_posts()) : $count = 1; while (have_posts()): ?>
<?php if ($count == 6) : ?>
// Paste your ad code here
<?php $count = 0; endif; ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php $count++; endwhile; endif; ?>