
{"id":987,"date":"2012-04-15T09:27:52","date_gmt":"2012-04-15T09:27:52","guid":{"rendered":"http:\/\/congnghepc.com\/2012\/04\/15\/dem-luot-xem-vai-viet-wordpress\/"},"modified":"2012-04-15T09:27:52","modified_gmt":"2012-04-15T09:27:52","slug":"dem-luot-xem-vai-viet-wordpress","status":"publish","type":"post","link":"https:\/\/tapchicntt.com\/dem-luot-xem-vai-viet-wordpress\/","title":{"rendered":"\u0110\u1ebfm L\u01b0\u1ee3t Xem B\u00e0i Vi\u1ebft &#8211; WordPress"},"content":{"rendered":"<p>\n\t\u0110\u1ea7u ti&ecirc;n, b\u1ea1n m\u1edf file <strong>functions.php<\/strong> ra v&agrave; th&ecirc;m v&agrave;o \u0111o\u1ea1n code sau.<\/p>\n<pre>\r\nfunction getPostViews($postID){ \/\/ h&agrave;m n&agrave;y d&ugrave;ng \u0111\u1ec3 l\u1ea5y s\u1ed1 ng\u01b0\u1eddi \u0111&atilde; xem qua b&agrave;i vi\u1ebft\r\n    $count_key = &#39;post_views_count&#39;;\r\n    $count = get_post_meta($postID, $count_key, true);\r\n    if($count==&#39;&#39;){ \/\/ N\u1ebfu nh\u01b0 l\u01b0\u1ee3t xem kh&ocirc;ng c&oacute;\r\n        delete_post_meta($postID, $count_key);\r\n        add_post_meta($postID, $count_key, &#39;0&#39;);\r\n        return &quot;0&quot;; \/\/ gi&aacute; tr\u1ecb tr\u1ea3 v\u1ec1 b\u1eb1ng 0\r\n    }\r\n    return $count; \/\/ Tr\u1ea3 v\u1ec1 gi&aacute; tr\u1ecb l\u01b0\u1ee3t xem\r\n}\r\n<\/pre>\n<p>\n\tSau khi \u0111&atilde; c&oacute; 2 h&agrave;m tr&ecirc;n th&igrave; vi\u1ec7c c&ograve;n l\u1ea1i l&agrave; ch&uacute;ng ta g\u1ecdi 2 h&agrave;m \u0111&oacute; \u0111\u1ec3 s\u1eed d\u1ee5ng th&ocirc;i.<br \/>\n\tTr\u01b0\u1edbc ti&ecirc;n, \u0111\u1ec3 c&oacute; \u0111\u01b0\u1ee3c l\u01b0\u1ee3t view th&igrave; b\u1ea1n c\u1ea7n \u0111\u1ec3 function <strong>setPostViews()<\/strong> trong file <strong>single.php<\/strong> v&agrave; n&oacute; ph\u1ea3i n\u1eb1m trong v&ograve;ng l\u1eadp while.<\/p>\n<pre>\r\n<!--?php if (have_posts()) : while (have_posts()) : the_post(); ?-->function setPostViews($postID) {\/\/ h&agrave;m n&agrave;y d&ugrave;ng \u0111\u1ec3 set v&agrave; update s\u1ed1 l\u01b0\u1ee3t ng\u01b0\u1eddi xem b&agrave;i vi\u1ebft.\r\n    $count_key = &#39;post_views_count&#39;;\r\n    $count = get_post_meta($postID, $count_key, true);\r\n    if($count==&#39;&#39;){\r\n        $count = 0;\r\n        delete_post_meta($postID, $count_key);\r\n        add_post_meta($postID, $count_key, &#39;0&#39;);\r\n    }else{\r\n        $count++; \/\/ c\u1ed9ng \u0111\u1ed3n view\r\n        update_post_meta($postID, $count_key, $count); \/\/ update count\r\n    }\r\n}\r\n<\/pre>\n<p>\n\tC&ograve;n h&agrave;m <strong>getPostViews()<\/strong> th&igrave; ch&uacute;ng ta c&oacute; th\u1ec3 \u0111\u1ec3 n&oacute; \u1edf m\u1ecdi n\u01a1i nh\u01b0 <strong>index.php, category.php<\/strong>, <strong>single.php<\/strong>&hellip; \u0111\u1ec3 n&oacute; hi\u1ec3n th\u1ecb s\u1ed1 l\u01b0\u1ee3t xem b&agrave;i vi\u1ebft, v&agrave; t\u1ea5t nhi&ecirc;n l&agrave; n&oacute; ph\u1ea3i n\u1eb1m trong v&ograve;ng while.<\/p>\n<pre>\r\n<!--?php if (have_posts()) : while (have_posts()) : the_post(); ?-->&lt;!--?php if (have_posts()) : while (have_posts()) : the_post(); ?--&gt;\r\n&lt;!--?php setPostViews(get_the_ID()); ?--&gt; &lt;!-- set v&agrave; update l\u1ea1i l\u01b0\u1ee3t views--&gt;\r\n&lt;!--?php the_title(); ?--&gt; &lt;!-- Title b&agrave;i vi\u1ebft --&gt;\r\n&lt;!--?php the_content(); ?--&gt; &lt;!--\/ N\u1ed9i dung b&agrave;i vi\u1ebft--&gt;\r\n&lt;!--?php endwhile; endif; ?--&gt; <!-- l\u1ea5y th\u00f4ng tin l\u01b0\u1ee3c views--><!-- Title b\u00e0i vi\u1ebft-->\r\n<\/pre>\n<p>\n\tV\u1eady l&agrave; v\u1edbi v&agrave;i thao t&aacute;c \u0111\u01a1n gi\u1ea3n, ch&uacute;ng ta c&oacute; th\u1ec3 t\u1ea1o ra \u0111\u01b0\u1ee3c 1 \u1ee9ng d\u1ee5ng nho nh\u1ecf cho WP, v&agrave; t\u1ea5t nhi&ecirc;n l&agrave; kh&ocirc;ng c\u1ea7n ph\u1ea3i d&ugrave;ng plug-in n&agrave;o h\u1ebft. Ch&uacute;c c&aacute;c b\u1ea1n vui v\u1ebb v\u1edbi b&agrave;i vi\u1ebft.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\n\t<img decoding=\"async\" alt=\"\" src=\"http:\/\/dl.dropbox.com\/u\/1552467\/tutorial\/wp_plugin.jpg\" style=\"float: left; width: 100px; height: 76px; margin-left: 5px; margin-right: 5px;\" \/>\u0110\u1ebfm xem s\u1ed1 l\u01b0\u1ee3t ng\u01b0\u1eddi \u0111&atilde; xem qua b&agrave;i vi\u1ebft c\u1ee7a m&igrave;nh tr&ecirc;n WP xem t\u01b0\u1edfng nh\u01b0 l&agrave; m\u1ed9t vi\u1ec7c \u0111\u01a1n gi\u1ea3n, nh\u01b0ng m&agrave; l\u1ea1i c\u0169ng kh&aacute; kh&oacute; kh\u0103n v\u1edbi m\u1ed9t s\u1ed1 b\u1ea1n.<\/p>\n<p>\tVi\u1ec7c \u0111\u1ebfm s\u1ed1 ng\u01b0\u1eddi xem b&agrave;i vi\u1ebft th&igrave; c&aacute;c b\u1ea1n c&oacute; th\u1ec3 s\u1eed d\u1ee5ng plug-in <a href=\"http:\/\/wordpress.org\/extend\/plugins\/wp-postviews\/\" target=\"_blank\">WP-Postviews<\/a>,<\/p>\n<p>\n\tV&agrave; b\u1ea1n c\u0169ng c&oacute; th\u1ec3 \u0111\u1ebfm s\u1ed1 l\u01b0\u1ee3t xem b&agrave;i vi\u1ebft m&agrave; kh&ocirc;ng c\u1ea7n d&ugrave;ng b\u1ea5t k&igrave; plugin n&agrave;o c\u1ea3, b\u1ea1n c&oacute; th\u1ec3 l&agrave;m theo c&aacute;c b\u01b0\u1edbc ph&iacute;a d\u01b0\u1edbi l&agrave; \u0111\u01b0\u1ee3c.<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[143],"tags":[],"class_list":["post-987","post","type-post","status-publish","format-standard","hentry","category-thu-thuat-wordpress"],"views":431,"_links":{"self":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/comments?post=987"}],"version-history":[{"count":0,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/posts\/987\/revisions"}],"wp:attachment":[{"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tapchicntt.com\/rest-api\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}