Felix
Felix

Reputation: 1583

Wordpress postcounter

How do I get a value of how many posts I have? Is there a wordpress tag for that?

I want to use it in my theme like this: Number of posts: 37

Upvotes: 0

Views: 55

Answers (2)

shrestha rohit
shrestha rohit

Reputation: 2940

This code may help you to show no of post in current page

<?php
$count = 1;
if (have_posts()) : while(have_posts()): the_post(); ?>
layout of regular template file here
<?php $count++;
      endwhile; endif;
echo $count;
 ?>

Alternatively you can use as shown in Wordpress Documentation wp_count_posts

<?php
$count_posts = wp_count_posts();
?>

Upvotes: 0

eric.itzhak
eric.itzhak

Reputation: 16062

Yes there is a function that does that :

$count_posts = wp_count_posts();

From the official Documentation :

The default usage returns a count of the posts that are published. This will be an object, you can var_dump() the contents to debug the output.

Or : Again qoute :

If you want to show the number of published posts use this code.

$published_posts = wp_count_posts();
echo $published_posts->publish;

I'm not sure which one is the right method, never tried before but seems like both will do the trick.

Upvotes: 1

Related Questions