I tried this way to display NO of post:
<?php
$news_2 = new WP_Query( array ('post_type'=> 'jobs','posts_per_page'=> '10' , 'meta_key' => 'status_for_jobs','meta_value' => '1') );
if ( $news_2->have_posts() ) { while ( $news_2->have_posts() ) { $news_2->the_post();
$count = $news_2->post_count;
?>
<li><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></li>
<?php } } ?> <?php wp_reset_query(); ?>
if the NO of post = 0 i need to display this :-
<?php
$news_2 = new WP_Query( array ('post_type'=> 'jobs','posts_per_page'=> '10' , 'meta_key' => 'status_for_jobs','meta_value' => '1') );
if ( $news_2->have_posts() ) { while ( $news_2->have_posts() ) { $news_2->the_post();
$count = $news_2->post_count;
if ($count == '0') {
?>
<li><h3><a href="javascript:void(0)">No Post</a></h3></li>
<?php
} else {
?>
<li><h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></li>
<?php } ?>
<?php } } ?> <?php wp_reset_query(); ?>
But instead of the total of posts, I not getting any thing.
Any suggestions to fix this?
Some additional information, no need to count the posts again, because
WP_Query
already did that for you. To clarify this, some information from the Class Reference ofWP_Query
as found in the »Properties« section:What this means is
$post_count
won’t give you the total post count. It will most likely give you the number of posts you’ve defined with thepost_per_page
parameter, unless you’ve fewer posts than that or you’re on the last page and there are only fewer posts left.$found_posts
can be used to get the total number of post related to a specific query. So there is no need to count them again.In your case you can get the total count into your
$count
variable like this:Besides of that @helgatheviking is right that, from what you’ve shown in your question, you don’t need a extra conditional, but can just use the
have_posts()
method, in the conditional you already have, for that, like she suggested.You don’t need to count the posts to show something different if no posts are found. You can just use the
else
part of yourif($news_2->have_posts())
check.But if you truly need to count the number of posts returned, you can use