{"id":3023,"date":"2010-01-18T12:29:54","date_gmt":"2010-01-18T10:29:54","guid":{"rendered":"http:\/\/blog.ginchen.de\/?p=3023"},"modified":"2010-05-04T01:51:11","modified_gmt":"2010-05-03T23:51:11","slug":"wordpress-statistiken-plugin-und-widgetfrei-ins-template-einbinden","status":"publish","type":"post","link":"http:\/\/blog.ginchen.de\/en\/2010\/01\/18\/wordpress-statistiken-plugin-und-widgetfrei-ins-template-einbinden\/","title":{"rendered":"Integrate WordPress statistics into a template plugin- and widget-free"},"content":{"rendered":"<p>For a while I&#8217;ve been looking for a way to integrate the top-posts of my site, that have been established by the <a  rel=\"external\" href=\"http:\/\/wordpress.org\/extend\/plugins\/stats\/\">WordPress stats plugin<\/a>, into my template. I wanted to have a list of the five most visited posts, preferably with the number of comments.<br \/>\r\nI remembered that I had once seen a plugin for that: <a  rel=\"external\" href=\"http:\/\/wordpress.org\/extend\/plugins\/wordpress-popular-posts\/\">WordPress Popular Posts<\/a>. But when I installed it, I found that in the latest version, it works exclusively as a widget and does not provide a template tag. That way, of course, it was of absolutely no use to me, because my template is not and will never be intended for widgets. And simply installing an older version of the plugin is not a permanent solution either.<\/p>\r\n\r\n<p>But as usual, I thought to myself: &#8220;Man, there must be some way to do this!&#8221; And there is one indeed! With just a few lines of code, you can integrate your top posts from the WordPress stats plugin at any place in your template, without any additional plugin!<\/p>\r\n\r\n\r\n<!--more-->\r\n\r\n\r\n\r\n\r\n<h3>Here&#8217;s how:<\/h3>\r\n\r\n<pre lang=\"php\" line=\"1\">\r\n<?php if(function_exists('stats_get_csv')) : ?>\r\n\t<h3>Popular Posts<\/h3>\r\n\t<ul id=\"popularposts\">\r\n\t<?php\r\n\t\t$top_posts = stats_get_csv('postviews', 'days=-1&#038;limit=5');\r\n\t\tforeach($top_posts as $post) {\r\n\t\t\tif(!get_post($post['post_id']) || empty($post['post_id'])) continue;\r\n\t\t\techo '<li><a href=\"'.get_permalink($post['post_id']).'\">'.get_the_title($post['post_id']).'<\/a><div class=\"commentsnumber\">('.get_comments_number($post['post_id']).' comments)<\/div><\/li>';\r\n\t\t}\r\n\t?>\r\n\t<\/ul>\r\n<?php endif; ?>\r\n<\/pre>\r\n\r\n<h3>Short description:<\/h3>\r\n<ul>\r\n<li><strong>Line 1:<\/strong> The entire list will be created only if the WordPress stats plugin is installed and activated.<\/li>\r\n<li><strong>Line 5:<\/strong> Here you define which or how many posts are shown. If you set &#8220;days&#8221; to -1, you get the top posts of all time, ever since the stats plugin has started collecting data. However, if you set it to 7, for example, you get the top posts of the last seven days. The value &#8220;limit&#8221; indicates how many posts you would like to display. So in the example, the five most popular posts are shown.<\/li>\r\n<li><strong>Line 8:<\/strong> Here we output the link with the number of comments. Of course you can adjust the HTML depending on your needs.<\/li>\r\n<\/ul>\r\n\r\n<ins datetime=\"2010-04-04T12:13:09+00:00\">\r\n<h3>Update (17 March 2010)<\/h3>\r\n<p>Someone asked if it is also possible to exclude certain pages from the top list. The stats plugin itself does not provide an &#8220;exclude&#8221; option. But of course there is a way:<\/p>\r\n\r\n<pre lang=\"php\">\r\n<?php if(function_exists('stats_get_csv')) : ?>\r\n\t<h3>Popular posts<\/h3>\r\n\t<ul id=\"popularposts\">\r\n\t<?php\r\n\t\t$top_posts = stats_get_csv('postviews', 'days=-1&#038;limit=10');\r\n\t\t$i = 0;\r\n\t\tforeach($top_posts as $post) {\r\n\t\t\tif(!get_post($post['post_id']) || empty($post['post_id']) || in_array($post['post_id'], array(1, 17, 25, 39))) continue;\r\n\t\t\techo '<li><a href=\"'.get_permalink($post['post_id']).'\">'.get_the_title($post['post_id']).'<\/a><div class=\"commentsnumber\">('.get_comments_number($post['post_id']).' Kommentare)<\/div><\/li>';\r\n\t\t\t$i++;\r\n\t\t\tif($i >= 5) break;\r\n\t\t}\r\n\t?>\r\n\t<\/ul>\r\n<?php endif; ?>\r\n<\/pre>\r\n\r\n<p>First you have to increase the limit at least by the number of excluded pages. If in doubt, rather set it a little too high than too low. Then enter the IDs of the pages you wish to exclude into the array, where I wrote &#8220;1, 17, 25, 39&#8221; as an example. If necessary, replace the 5 in the line <code>if($i &gt;= 5) break;<\/code> by the desired number of top sites, in case you wish to display more or less than five top posts.<\/p>\r\n<\/ins>","protected":false},"excerpt":{"rendered":"For a while I&#8217;ve been looking for a way to integrate the top-posts of my site, that have been established by the WordPress stats plugin, into my template. I wanted to have a list of the five most visited posts, preferably with the number of comments. I remembered that I had once seen a plugin [&hellip;]","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77],"tags":[429,430,606,163],"_links":{"self":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/3023"}],"collection":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/comments?post=3023"}],"version-history":[{"count":26,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/3023\/revisions"}],"predecessor-version":[{"id":3047,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/posts\/3023\/revisions\/3047"}],"wp:attachment":[{"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/media?parent=3023"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/categories?post=3023"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.ginchen.de\/en\/wp-json\/wp\/v2\/tags?post=3023"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}