Conclusion to the main popular articles. WordPress post view counter: installation and configuration. How to choose the right plugin

Continuing the topic of useful improvements for WordPress, I will tell you how to take into account page visit statistics and display number of post views.

This can be done in two ways:

1. Install the third party plugin WP-PostViews
2. Add a Few Lines of Code to a WordPress Template

First, I will talk about both methods in more detail, and then, comparing them with each other, I will draw conclusions and list the advantages and disadvantages of each of these methods.

Displaying the Number of Post Views in WordPress Using the Wp-PostViews Plugin

It happens as usual. After installation, the plugin must be activated.

Key features of the WP-PostViews plugin:

  • displaying the number of page and post views;
  • the ability to take into account only guests, only registered users or all;
  • the ability to ignore views by a search robot;
  • displaying a list of recently viewed pages;
  • displaying a list of the last viewed pages by tags;
  • displaying a list of the last viewed pages by category;
  • displaying a list of the most visited pages;
  • displaying a list of the most visited pages by category;
  • the ability to show the number of views only to registered users.

As you can see, the functionality of the WP-PostViews plugin is not small at all. However, do not forget about its main function - collecting and issuing statistics on the number of page and post views. And now we will see how this plugin copes with this task.

To do this, you need to insert the following code

in Right place in your theme's code file.

For example, if you want the number of views to be displayed on the post page immediately after its text, you need to insert the code after the function to call the text in the file single.php

In the same way, you can paste this code into:

  • index.php- for display on the main page;
  • page.php- for display on pages;
  • archive.php- for output in archives.

In plugin settings

you can change some functions.

Now for how to use additional functions plugin WP-PostViews.

In order to display a list of the most viewed posts, you need to insert the following code

The result will look like this

By default, the top 10 most viewed posts are displayed. In order to change this number, it is necessary to define parameters in the function get_most_viewed.

For example, to display a list of the five most popular posts, change the code like this

To display the last viewed posts, you need to insert the following code

to display the most popular posts by tags −

to display the most popular posts in a category −

.

Displaying Post View Count in WordPress Without a Plugin

Now let's look at the second way to display the number of page views by tampering with the WordPress template code.

Add to file functions.php following code

It is better to add the code to the end of the document, and mark it with comments, so that if it is not needed, it can be easily found and deleted.

The code represents two functions, one of which collects statistics, and the other is responsible for its output.

The next step is to add to the files ( single.php, page.php etc.), in which you need to count views the following code

and the final step is to paste the code

For example, I placed this code before the text of the entry. In the end it looks like this

Displaying the number of views of a post in WordPress. conclusions

  • great functionality;
  • output of popular records (additional relinking);
  • flexible settings;
  • periodic updates.

Flaws:

  • limited functionality.

Advantages:

Conclusion: if your server allows you to increase the load, then the best option for you to install the WP-PostViews plugin, due to its great functionality, and additional relinking. Otherwise, use the second method.

Plugin page on wordpress.org.

But if your knowledge of the PHP programming language is still too small, you can find a lot useful information on the php forum. This will help you in further work with wordpress.

Hello dear blog readers. Today we will learn how to display a list of the most popular entries on, and most importantly, that these entries will be with thumbnails. Naturally, we will not use any plugins. Just a few lines of php code and CSS styling.

I would like to say that such a list, as it turns out, is done very simply. But loads the server a little. Although if you do not display more than 5 records, then there is practically no load.

The list of popular posts will be sorted by the number of comments on the post. It turns out that the more comments in the post, the more popular it is and the very first one in the list will be displayed.

Well, now let's get started.

List of Most Popular WordPress Thumbnail Posts Without Plugins

PHP entry code

As I said above, there is nothing complicated here. This list is displayed using WP_Query, with absolutely no complicated and no big settings. So this line will look like this:

have_posts()) : $pc->the_post(); ?>

And after that, it remains to add only the variable names of the post, as well as its thumbnail:

"title="(!LANG:!}">"title="(!LANG:!}">

So here's what we got:

    have_posts()) : $pc->the_post(); ?>
  • "title="(!LANG:!}">"title="(!LANG:!}">

Here you can change the number of output entries, it changes here posts_per_page=5, in this case there will be 5 posts.

You see how simple everything is :-) But that's not all. Now we need to decorate the list a little. You can see an example on my blog in the sidebar.

Decorating the list with CSS.

First we need to come up with some kind of class, for example popular. And now for this class we will set the following settings:

Popular ul li( line-height:16px; display:block; border-bottom:1px solid #e9e9e9; padding:15px 0px 20px 0px; height:50px; font-size:12px; )

As you can see, we have also set the settings for the ul li lists in which our popular posts will be surrounded. Here I've added a line that separates the list, then set the indents, which you can change for yourself.

Now let's set the settings for our thumbnails.

Popular ul li img ( width: 50px; height: 50px; border:1px solid #ccc; background:#fff; padding:2px; float:left; margin:0 10px 0 0px; -moz-box-shadow: 0 0 10px rgba(0,0,0,0.2); /* For Firefox */ -webkit-box-shadow: 0 0 10px rgba(0,0,0,0.2); /* For Safari and Chrome */ box-shadow: 2px 2px 2px rgba(0,0,0,0.1); /* Shadow Options */ -moz-transition: border 0.3s 0.05s ease; -o-transition: border 0.3s 0.05s ease; -webkit-transition: border 0.3s 0.05s ease;) .popular li img:hover ( border:1px solid #888; )

Here, the thumbnails are framed, which gradually darkens when hovered over. This is done with transition and .popular li img:hover. A small shadow for the images has also been added, so it looks more beautiful than without it. The default size is 50 by 50 pixels. Naturally, you can easily change this value.

Now let's see how it will look in , and also find out where to insert this code.

Where to paste code in WordPress?

In the admin panel of your blog, you need to go to the editor, then we find Sidebar (sidebar.php) and paste the following at the desired location:

Popular ul li( line-height:16px; display:block; border-bottom:1px solid #e9e9e9; padding:15px 0px 20px 0px; height:50px; font-size:12px; ) .popular ul li img ( width: 50px ; height: 50px; border:1px solid #ccc; background:#fff; padding:2px; float:left; margin:0 10px 0 0px; -moz-box-shadow: 0 0 10px rgba(0,0,0, 0.2); /* For Firefox */ -webkit-box-shadow: 0 0 10px rgba(0,0,0,0.2); /* For Safari and Chrome */ box-shadow: 2px 2px 2px rgba(0,0 ,0,0.1); /* Shadow options */ -moz-transition: border 0.3s 0.05s ease; -o-transition: border 0.3s 0.05s ease; -webkit-transition: border 0.3s 0.05s ease; ) . popular li img:hover ( border:1px solid #888; )

That's all, now popular entries with thumbnails are displayed in your sidebar :-) Friends, if you have any questions, ask in the comments, see you soon.

Some time ago we talked about how. However, I would like to configure the arrangement of elements as convenient as possible for the user. In today's article, we will show you how to track and display the most viewed posts on your WordPress blog without the use of plugins.

An example of popular self-produced records in the screenshot below:

The first thing we need to do is create a function that will determine the number of views of a post and store it in a custom field for each post. To do this, paste the following code into your theme's functions.php file or, better, into :

Function devise_set_post_views($postID) ( $count_key = "devise_post_views_count"; $count = get_post_meta($postID, $count_key, true); if($count=="")( $count = 0; delete_post_meta($postID, $count_key ); add_post_meta($postID, $count_key, "0"); )else( $count++; update_post_meta($postID, $count_key, $count); ) ) remove_action("wp_head", "adjacent_posts_rel_link_wp_head", 10, 0);

Now that the function is in place, we need to call it on individual blog posts. In this way, the function will know exactly which posts need to increase the view count. To do this, nueno insert the following code into the loop for a single entry (most likely in your theme's single.php file):

Devise_set_post_views(get_the_ID());

If you are using a child theme, or just want to keep things simple, then you need to add a tracking tracker to the header using the wp_head hook. Paste the following code into your theme's functions.php file or into :

Function devise_track_post_views ($post_id) ( if (!is_single()) return; if (empty ($post_id)) ( global $post; $post_id = $post->ID; ) devise_set_post_views($post_id); ) add_action("wp_head ", "devise_track_post_views");

After the code is in the right place, each time a single user opens a record, the counter in the user field will be updated.

Note A: If you are using a caching plugin on your blog, this will NOT work by default. The W3 Total Cache plugin has a feature called Fragmented Caching. You need to use it, and the problem will disappear. You need to change the following:

Now you can do various interesting things, such as displaying the number of views of a post, or sorting by these very views ... Let's see how they can be implemented:

Function devise_get_post_views($postID)( $count_key = "devise_post_views_count"; $count = get_post_meta($postID, $count_key, true); if($count=="")( delete_post_meta($postID, $count_key); add_post_meta($ postID, $count_key, "0"); return "0 views"; ) return $count." views"; )

Then insert the following inside the write loop:

Devise_get_post_views(get_the_ID());

If you need to sort posts by the number of views, this can be done using the post_meta wp_query parameter. The simplest example of a request loop would look like this:

4, "meta_key" => "devise_post_views_count", "orderby" => "meta_value_num", "order" => "DESC")); while ($popularpost->have_posts()) : $popularpost->the_post(); the_title(); endwhile; ?>

For adding other WP_Query parameters such as time range, refer to

One of the most popular ways to improve the behavioral factors on the site is to use the "Popular Posts" block. It allows you to keep the visitor's attention on the most interesting entries. In general, it is very good for promoting your site.

Today we will look at how to display popular posts without using plugins. Personally, I don't really like when the site is overloaded with them. Before we get into the technical implementation, let's focus on one common mistake.

popularity theory

Usually, to implement the above task, developers resort to a simple algorithm: display publications by the number of views or comments. More views/comments = more popularity. Everything seems to be simple and logical.

But in my opinion, using such a ranking algorithm, we make a small mistake. The fact is that in this case we do not take into account the time factor. This means that a post, say posted on the site a year ago, is likely to have more views or comments than one posted yesterday. Even if the new one is objectively better and more interesting than the older one. Thus, old publications, by coincidence, will forever be registered in the "Popular entries" block.

This idea was prompted by one well-known site dedicated to working in Photoshop. So, in the "Popular" block, lessons are displayed that are qualitatively worse than newer ones. But, due to their earlier appearance, they have more views. The list of popular entries has not changed for more than a year and a half. Do you need it too?

As for the popularity of the comments. A number of questions arise. What if there are no comments on the site yet? How to rank if the criterion of popularity is only comments? There are more questions than answers.

Creating a Popularity Algorithm

Let's try to eliminate all these shortcomings, allowing newer, really high-quality materials to become popular. Our algorithm is just a blueprint that will allow you to create a more thoughtful ranking system. If your site has several hundred or thousands of articles, you can conduct interesting experiments. Anyway. Let's get started.

Popularity Index = Views / Days of Existence

Let's figure it out. Based on the formula, a post "A" that has been around for 100 days and has 100 views will be less popular than a newer post "B" that has 10 views in 1 day. This is logical, because theoretically, the post "B" in the same 100 days will gain significantly more views than its competitor.

However, this formula does not take into account one more point. 100 days ago, the website traffic could have been only 1 person per day, and at the time of the publication of post "B" - 100 people per day. Let's introduce another ranking factor. This is the comment indicator. It is logical, because if the publication is discussed, then it is interesting. Our formula will look like this:

Popularity Index = Comments + Views / Days of Existence

So, in our algorithm the following factors will be taken into account:

  1. number of post views
  2. number of days the publication exists
  3. number of comments

Now, having decided on the formula, let's start implementing the output of records according to the created popularity index.

Record output

Let's create a blank that will serve as a starting point. We display 5 records. Place the following code where you want to display posts.

Now you can decorate it nicely. This code should be placed in your theme's style.css file.

#popular_posts ul ( margin-left:10px; ) #popular_posts ul li( display:block; border-bottom:1px solid #f1f1f1; padding:15px 0px 20px 0px; height:50px; font-size:13px; ) #popular_posts ul li img ( width: 50px; height: 50px; float: left; margin: 0 13px 0 0px; background: #fff; border: 2px solid #f1f1f1; border-radius: 50%; ) #popular_posts ul li p ( margin- top:10px; )

Calculate the popularity index

Page views

Function setPostViews($postID) ( $count_key = "views"; $count = get_post_meta($postID, $count_key, true); if($count == "")( $count = 0; delete_post_meta($postID, $count_key ); add_post_meta($postID, $count_key, "0"); ) else ( $count++; update_post_meta($postID, $count_key, $count); ) ) function getPostViews($postID)( $count_key = "views"; $ count = get_post_meta($postID, $count_key, true); if($count=="")( delete_post_meta($postID, $count_key); add_post_meta($postID, $count_key, "0"); return "0"; ) return $count; )

Be sure to count each visit to the page. It must be placed in a single entry file, usually single.php

Just in case, if you suddenly need to display in the frontend:

Views:

Counting the number of days a post exists

// get post date in seconds since January 1, 1970 $dtNow = get_post_time("U", true, $postid); // get current time $dtTime = current_time("U"); // count the difference in days $diff = $dtTime - $dtNow; $days = $diff/86400;

Counting the number of comments

comments_number("0", "1", "%");

So, we have determined how we will receive the data of each entry to calculate the popularity index.

Display posts by popularity

There is one important point. If we start recalculating the popularity index every time someone visits the site, this will create a rather high load. The site may be slower. In order not to create unnecessary load, let's schedule a task that will allow us to recalculate once a day. Let's use the wp_schedule_event() scheduler. Place the code below in the function.php file:

// add the scheduled hook add_action("wp", "my_activation"); function my_activation() ( if(! wp_next_scheduled("my_daily_event")) ( wp_schedule_event(time(), "daily", "my_daily_event"); ) ) // add a function to the specified hook function do_this_daily() ( global $wpdb; $ postids = $wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE post_status="publish" AND post_type="post" ORDER BY ID ASC"); foreach($postids as $postid)( $postid = $postid ->ID; // Post ID // count the number of views $views = (int)get_post_meta($postid, "views", true); // count the days of the post existence //$dtNow = get_the_time("U"); $ dtTime = current_time("U"); $diff = $dtTime - $dtNow; $dtNow = get_post_time("U", true, $postid); $dtTime = current_time("U"); $diff = $dtTime - $ dtNow; // calculate the comments and the sum of views with comments $comments = get_comments_number($postid); $summa = $views + $comments; // calculate the popularity index if ($days = "0")( $pop_index = $summa / 1; ) else ( $days = (int)$diff/86400; $pop_index = $summa / $days; ) $ pop = round($pop_index, 2); // write the popularity index to a custom post field update_post_meta($postid, "popularity", $pop); ) ) add_action("my_daily_event", "do_this_daily", 10, 2);

Now the output of the posts.

Output via shortcode

For convenience, let's try to display posts through the [ popular-posts ] shortcode (don't forget to remove the spaces).

Function popular_posts() ( ob_start(); ?>

Options ob_start() and ob_get_clean() allows us to output the loop where we want. If they are not added, the list will simply appear at the top until all other content is loaded.

Instead of totals

As you can see, there is nothing complicated in displaying posts by popularity. On the other hand, we got a fully working feature that will allow us to display publications that are really interesting to readers. Of course, if desired, this algorithm can be improved. If you have suggestions and ideas, write in the comments.

2 votes

Good day, dear readers of my blog. On any site there are articles that are read more often than others. The reason for this lies not only in the popularity of the search phrase, but also in the quality of the publication itself. Anyone can be in the top of the search engine, but the final decision is made by the audience. Something did not like, and the reader leaves the blog.

A little hint for a new person on your site will help him find the best and most useful posts on your site. He may come by chance to the site and suddenly stumble upon something interesting and stay. This is good for you as a developer. After all, then the time spent on the blog will increase.

Today we will talk about WordPress popular posts. Let's learn how to display them in simple ways, using widgets, to achieve a beautiful result quickly. I will also give some useful links to those beginners who have long been striving for independence and independence.

Shall we start?

Easy withdrawal method

To begin with, I would like to make a short overview of popular plugins, then I will tell you how to work with them using another simple option as an example. By the way, in my opinion, simplicity in this case does not in any way go to the detriment of the quality of design. I liked this extension even more than some of the main ones.

I think that many people will find something interesting here: both completely inexperienced beginners striving for simplicity and style, and quite mature users who are eager to create their own, independent and unique projects based on someone else's code.

Easy Popular Posts

You can install the widget in the text part (content column), the main panel or placed in the footer (footer).

This is what the settings panel looks like. Articles can be displayed with or without pictures. If there are a lot of links on the page and you are afraid of spam, then you can put the nofollow tag so that search engines do not take them into account.

Popularity can be determined by the number of comments, views or reposts. I already told you how to install the appropriate ones.

WordPress Popular Posts

Huge number of downloads, incredibly easy installation and setup, and a huge selection of options. You can customize the display of popular in a specific category on the site.

An impressive menu, which is quite easy to understand, even without knowing English.

You yourself set the number of input posts, the frequency of updates.

Various display options are possible. Without pictures or with illustrations. What size will they be. Show number of views, author, category. How big will the block itself be, etc.

You can put posts that are popular today during the week, month, year. Depending on the age of the resource.

If you want, you can display article descriptions by word or character count.

Open source. If you wish, you can study Free CSS Course (45 Video Lessons!) from Evgeny Popov and customize the block to your liking.

Popular Widget

Another interesting plugin is Popular Widget.

And again a large number of settings. It counts the number of views, which creates a list of popular articles on your blog.

Shows not only the most popular articles, but also the most recently viewed posts, comments, tags and more.

The code, again, is open.

How to display news

Let me now show you how to display news and configure plugins. I will act on the example of Popularity Posts Widget. It is less functional than the previous ones. Thanks to him, I will be able to show beginners how to work with widgets and will not write a three-volume article for this purpose. Perhaps someone will like it.

I find the Popularity Posts Widget through the "Plugin" - "Add New" and activate it. With this, I am sure that there will be no problems. If anything, I already gave a link to the desired publication.

Open "Appearance" - "Widgets".

Choose where the block will be placed. You can try all three options in turn: main column, content, or footer.

As soon as you click on "Add Widget", an additional window will appear on the left. It pops up instantly and grabs your attention.

Change the title, set up selection conditions. If the site is old, and you set a long sampling period, the same articles with thousands of views will be placed on top.

We also select the number of characters in the title and what will be displayed (date, comments, number of views).

Now popular records look like this. The pictures are too small in my opinion. How to change the situation?

I was always surprised how designers boldly and quickly determine by eye what size of a particular block should be. Of course, over the years of work, you can easily find a good eye. But the trial and error method does not appeal to me. I will spend a lot of time.

I downloaded the MeasureIt browser extension. This is a pixel line. I simply click on the icon in the browser, then select the area where the illustration should be.

It does not matter at what scale you are viewing the page, the indicators are displayed in pixels.

We return to the settings panel and set new values.

Now the pictures are displayed in a completely different way.

If you liked this article, I suggest you read, which display the latest (fresh) entries and publications in random order (arbitrary). You will be able to add news to your site from other sites: from Yandex, prestigious sources related to the career guidance of the portal.

Subscribe to the newsletter from my blog and receive useful publications with enviable frequency. Today you have an idea to create an additional block with useful publications, but how much more interesting things can be done to improve your blog, make working with it easier, more interesting!

Let me give you a new idea for development. Well, if you don't want to wait, I can offer a book “Promotion. Secrets of effective website promotion» . There is everything a developer needs here: work on and methods for protecting texts from theft, monitoring the behavior of visitors and working with data, in modern conditions, and much more.


Why is this book useful? There is a lot of information about Yandex here. We know quite a bit about him. There is much more information about Google in books and magazines. But now, when Yandex is becoming more powerful, more useful for Russian users and is increasingly pushing Google out of the vastness of the Russian Internet, it's time to study this particular search engine in more detail.

How long it will take and which methods to use is up to you.

OK it's all over Now. See you again. Subscribe to the newsletter and read useful books. I wish you good luck in your endeavors.