Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ.
You can simply do it without a plugin.
To count the post views, the first thing you have to do is to add the following code to your WordPress Theme functions.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<span class="hljs-meta"><?php</span> <span class="hljs-comment">/* * Set post views count using post meta//functions.php */</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">customSetPostViews</span>(<span class="hljs-params"><span class="hljs-variable">$postID</span></span>) </span>{ <span class="hljs-variable">$countKey</span> = <span class="hljs-string">'post_views_count'</span>; <span class="hljs-variable">$count</span> = get_post_meta(<span class="hljs-variable">$postID</span>, <span class="hljs-variable">$countKey</span>, <span class="hljs-literal">true</span>); <span class="hljs-keyword">if</span>(<span class="hljs-variable">$count</span>==<span class="hljs-string">''</span>){ <span class="hljs-variable">$count</span> = <span class="hljs-number">0</span>; delete_post_meta(<span class="hljs-variable">$postID</span>, <span class="hljs-variable">$countKey</span>); add_post_meta(<span class="hljs-variable">$postID</span>, <span class="hljs-variable">$countKey</span>, <span class="hljs-string">'1'</span>); }<span class="hljs-keyword">else</span>{ <span class="hljs-variable">$count</span>++; update_post_meta(<span class="hljs-variable">$postID</span>, <span class="hljs-variable">$countKey</span>, <span class="hljs-variable">$count</span>); } } <span class="hljs-meta">?></span> |
And now we will call this function in the single.php to update the count value in the Database.
1 2 3 4 |
<span class="hljs-meta"><?php</span> customSetPostViews(get_the_ID());<span class="hljs-comment">//single.php</span> <span class="hljs-meta">?></span> |
Now in the same single.php file if we want to show the post view count, we can use this code:
1 2 3 4 5 6 7 8 |
<span class="hljs-meta"><?php</span> <span class="hljs-variable">$post_views_count</span> = get_post_meta( get_the_ID(), <span class="hljs-string">'post_views_count'</span>, <span class="hljs-literal">true</span> ); <span class="hljs-comment">// Check if the custom field has a value.</span> <span class="hljs-keyword">if</span> ( ! <span class="hljs-keyword">empty</span>( <span class="hljs-variable">$post_views_count</span> ) ) { <span class="hljs-keyword">echo</span> <span class="hljs-variable">$post_views_count</span>; } <span class="hljs-meta">?></span> |
Now to show all the popular post in the descending order by post view count. use this code:
1 2 3 4 5 6 7 8 9 10 11 |
<span class="language-php"><span class="hljs-meta"><?php</span><span class="hljs-comment">//popular post query</span> query_posts(<span class="hljs-string">'meta_key=post_views_count&posts_per_page=5&orderby=meta_value_num& order=DESC'</span>); <span class="hljs-keyword">if</span> (have_posts()) : <span class="hljs-keyword">while</span> (have_posts()) : the_post(); <span class="hljs-meta">?></span></span><span class="language-xml"> <span class="hljs-tag"><<span class="hljs-name">li</span>></span><span class="hljs-tag"><<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"</span></span></span><span class="language-php"><span class="hljs-meta"><?php</span> the_permalink(); <span class="hljs-meta">?></span></span><span class="language-xml"><span class="hljs-tag"><span class="hljs-string">"</span>></span></span><span class="language-php"><span class="hljs-meta"><?php</span> the_title(); <span class="hljs-meta">?></span></span><span class="language-xml"><span class="hljs-tag"></<span class="hljs-name">a</span>></span><span class="hljs-tag"></<span class="hljs-name">li</span>></span> </span><span class="language-php"><span class="hljs-meta"><?php</span> <span class="hljs-keyword">endwhile</span>; <span class="hljs-keyword">endif</span>; wp_reset_query(); <span class="hljs-meta">?></span></span> |
Happy Coding
Bài viết này thực hiện (hoặc lụm bài về đăng câu like từ các trang khác) bởi Việt Lâm Coder một YOUTUBER có tâm và đẹp trai siêu cấp vô địch zũ trụ.