How to Show Total Posts in WordPress

Displaying the total number of posts in WordPress can be a necessity for many website administrators. Not only does this help keep track of content, but it can also be useful for visitors who want to know how many posts are available. In this article, we'll explore different methods for displaying the total number of posts on your WordPress site, including options through plugins and custom code.

Methods to display the total number of publications

There are several ways to display the total number of posts in WordPress. Below, we will analyze the most effective ones.

Use of plugins

If you are not comfortable working with code, the easiest option is to use a plugin. There are several plugins available in the WordPress repository that can help you display the total number of posts without writing a single line of code.

  • WP Statistics: This plugin not only displays the number of posts, but also provides detailed statistics about your site's traffic.
  • Simple Blog Stats: It allows you to display various statistics, including the total number of publications, in a very visual way.
  • Count per Day: Although it is best known for its visitor tracking, it also offers a widget that shows the total number of posts.

To install a plugin, simply go to the WordPress administration panel, select "Plugins" and then "Add New". Find the name of the plugin you want to install and click "Install Now". Once installed, activate the plugin and follow the instructions to configure it.

Custom code in the theme

If you prefer a more customized solution, you can add some code to your theme. This will allow you to display the total number of posts anywhere on your site, such as in the header, footer or in a widget.

Below is an example of code you can use:

publish;
echo "Total publications: " . $publications_total;
?>

To implement this code, follow these steps:

  1. Log in to your WordPress administration panel.
  2. Go to "Appearance" and then to "Theme Editor".
    How to Show Total Posts in WordPress
    Download our web maintenance guide Free of charge!
    Free guide for freelancers and small businesses that want to avoid surprises and improve their web performance.
  3. Select the "functions.php" file of your active theme.
  4. Paste the code where you want the total number of publications to be displayed.
  5. Save the changes.

This code counts all the posts published on your site and displays them in the place where you pasted it.

Show the total number of publications in the widget

If you want to show the total number of posts in a widget, you can do it using the same code mentioned above, but with some modifications to adapt it to a widget.

Create a custom widget

Below is an example of how to create a custom widget to display the total number of publications:

 __('Shows the total number of posts on the site', 'text_domain'))
        );
    }

    public function widget($args, $instance) {
        $publications_total = wp_count_posts()->publish;
        echo $args['before_widget'];
        echo $args['before_title'] . __('Total posts: ', 'text_domain') . $publications_total . $args['after_title'];
        echo $args['after_widget'];
    }
}

function register_total_posts_widget() {
    register_widget('Total_Posts_Widget');
}
add_action('widgets_init', 'register_total_posts_widget');
?>

This code creates a widget that you can add to your sidebar or any widget area of your theme. Once you have added the code to your theme's "functions.php" file, you can find the widget in the "Appearance" -> "Widgets" section.

Design customization

Once you have implemented the code or plugin to display the total posts, you may want to customize the layout to fit the style of your site.

CSS Styles

You can add custom CSS styles to enhance the appearance of the total posts. For example:


.total-publications {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

Then, you can apply this CSS class to the text that displays the total number of posts in your PHP code. This will make it look more attractive and in line with the design of your site.

Final considerations

Displaying the total number of posts in WordPress is a simple task that can be done through plugins or custom code. Both options offer flexibility and allow you to tailor the functionality to your site's needs. Whether through a widget, in the header or footer, having this information visible can be valuable to visitors and to you as the site administrator. Explore the options and choose the one that best suits your skills and needs.