Blog Design

Unknown | 7:17 PM | Be the first to comment!

How to Display Popular Posts by Views in WordPress without a Plugin


First thing we need to do is create a function that will detect post views count and store it as a custom field for each post. To do this, paste the following codes in your theme’s functions.php file or better in a site-specific plugin:
01function wpb_set_post_views($postID) {
02    $count_key 'wpb_post_views_count';
03    $count = get_post_meta($postID$count_key, true);
04    if($count==''){
05        $count = 0;
06        delete_post_meta($postID$count_key);
07        add_post_meta($postID$count_key'0');
08    }else{
09        $count++;

10        update_post_meta($postID$count_key$count);
11    }
12}
13//To keep the count accurate, lets get rid of prefetching
14remove_action( 'wp_head''adjacent_posts_rel_link_wp_head', 10, 0);
Now that you have this function in place, we need to call this function on the single post pages. This way the function knows exactly which post gets the credit for the views. To do this, you would need to paste the following code inside your single post loop:
1wpb_set_post_views(get_the_ID());
If you are using a child theme or you just want to make things easy for yourself, then you should simply add the tracker in your header by using wp_head hook. So paste the following code in your theme’s functions.php file or the site-specific plugin:




How to Display Random Posts in WordPress

A Single Random Post

The Second method i’ll show you is by adding a custom item to your navigation menu that gives that reader a random post to read. To be able to achieve this, you need to add two functions to your theme’s functions.php
The first one will generate the url for the random post; different on each page load.
  • function random_post() {
  • $posts = get_posts('post_type=post&orderby=rand&numberposts=1');
  • foreach($posts as $post) {
  • $postlink = get_permalink($post);
  • }
  • return "<a href='" . $postlink . "' title='" . get_the_title($post) . "'>Random Post</a>";
  • }
The second one, will add the link to your navigation bar. Simply add the code below to your functions.php and change the ‘ ‘ to yours
  • add_filter('wp_nav_menu_items','add_custom_in_menu', 10, 2);
  • function add_custom_in_menu( $items, $args ) {
  • // Don't forget to chnage "primary-menu" to the ID of your navigation menu
  • if( $args->theme_location == 'primary-menu') {
  • $items .= '<li>' . random_post() . '</li>';
  • }
  • return $items;
  • }
I hope this helps you Display Random Posts in WordPress, and to get back those old and lost posts from your posts archive.

How to Add Stylish Social Sharing Widget With Hover Effect For Blogger

1) Go to Blogger Dashboard → Layout → Page Elements.
2) Add a HTML/JavaScript Gadget.
3) Paste below code in it.  
  
<style type="text/css">
.social-connect-widget{background:#FFF;border:1px solid #E7E6DE;padding:10px}
.social-connect-widget:hover{border:1px solid #CBCBC2;background:#F5F4EF}
.social-connect-widget a{text-decoration:none;font-weight:bold;font-family:Arial,Helvetica,Sans-serif;color:#4B4D4E}
.social-connect-widget a:hover{text-decoration:underline;color:#F77C04}
.social-connect-widget img{vertical-align:middle;margin-right:5px}
</style>
<div class='social-connect-widget' style='margin-top:0px;margin-bottom:10px;'>
<a href='http://feedburner.google.com/fb/a/mailverify?uri=USERNAME1' target="blank"><img alt='RSS Feed' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHAMz7viBGF1AzGFUQsHsaThonX1yvzjLZtZZhWrmHFxamXxvCRK7yNwLezuFnfuqX4wpeUzzUvICmB4zPQHDq7oBhXEbqacaiYKKLignTAR_V6e9EnsW-1M9h-FW6ZpH2fT9puBbbsZY/h120/Pc+Tools+Tips.png' title='RSS Feed'/></a><a href='http://feedburner.google.com/fb/a/mailverify?uri=USERNAME1' target="blank">Subscribe to our RSS Feeds!</a>
</div> 
<div class='social-connect-widget' style='margin-bottom:10px;'>
<a href='http://twitter.com/USERNAME2' target="blank"><img alt='Follow Us on Twitter!' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYM4bDMS2TERfYFcGp1a5-qJbs9t_WwlHBGhK8jjAulfueugPe1s-6CyAQqEFqEwt3rsHc1Yb2B-fhGA6jEPPzyQqddOTsoYyYkXroXQNr1vwLWb_09OujCK2KKyFzoX40dPwk4oDXTNA/h120/Pc+Tools+Tips.png' title='Follow Us on Twitter!'/></a><a href='http://twitter.com/USERNAME2' target="blank">Follow Us on Twitter!</a>
</div>
<div class='social-connect-widget' style='margin-bottom:10px;'>
<a href='URL HERE' target="blank"><img alt='Be Our Fan!' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9OQUaW6YKxCXlwixDq0RZA2OLTDqokEfE4YWJJe2nPA_s8Dnikg5taPO-m9x6i-w0dFSAD3oqhTWQLkcURpBrzZKKCQz0-C1phCqez7pzi8jP5JpLxADVJEVE9pVUMMgJ0ryT8MxDnV0/h120/Pc+Tools+Tips.png' title='Be Our Fan'/></a><a href='URL HERE' target="blank">Follow us on Facebook!</a>
</div>
<div style='clear:both;'/>
<div class='addthis_toolbox'> 
<div class='custom_images'>
<a class='addthis_button_twitter'><img alt='Twitter' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhjhmlAZVfZaH2DdGeniZcnOxyg84mRSAZncGNp-j91v4naJB10SUft-XN1NAxYuIL63bTr-0eitVXU1O_tgF_227RrnOcc-YqX_DpOiJiD7Nw69gp0cKYUeZkaKCLZWHo9rwEoSW1G0-g/s1600/coolworld.in.png' width='32'/></a>
<a class='addthis_button_delicious'><img alt='Delicious' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiww_v1VFe0MQoXokW9NVGEsEIw_w82KBE8_3XJM973Ea3Fnym9PphYJqXOGX_MFh6r7XDfBXYI9WJGuqY79qv-Jt2_skF-vYmLl1BDqZ0pTD2qdhAKzHJw9TnPfdHCqGQwUmC5riVQbBg/s1600/coolworld.in.png' width='32'/></a>
<a class='addthis_button_facebook'><img alt='Facebook' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiN2pDEvrl9r0_MW1P5a1urkjGpmHPcYGxoOdQIS2eFxxhJpy8mvidgmsjqd4jKZF2Z9FiE0hHCIn-HMCOauJttJuxZ0DDWQdSIQ0Q8BhUj-CfFHXESPfxxdCsdBpXU2B9uvcb5ZuntJxc/s1600/coolworld.in.png' width='32'/></a>
<a class='addthis_button_digg'><img alt='Digg' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj55YjdCsVcaVzIPo1hbhpb8mu5vfoHYjk9JsxZwIGgiheqPd7MdrEOe4EbUCJv0qX3WnaejONy6dcrEcI1HqqQjwf3-x3uyjvAqezPhMTHYFPxQZHnkzmzYCteq0FIHS1q9Yfmll4vqt4/s1600/coolworld.in.png' width='32'/></a>
<a class='addthis_button_stumbleupon'><img alt='Stumbleupon' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg88gZiCLCzspX9nXLcT6VMB1XI8B3JwTnBTU4PzKSNnE8DvRXgz2MPuyDf8OMkWTBskQKMwxptLuH0MyDSeWgCOtPnPCy11f_Y27-LWyzEG70j0uBpuFoEtkWiO09iq2k9wNh6Wvjcm94/s1600/coolworld.in.png' width='32'/></a>
<a class='addthis_button_favorites'><img alt='Favorites' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibr9BiwuwSMX3rXOy1EQ1IXw2hWGZFxj-o5EzTjdpzcWlRy6TMKXlO68iaSAIC5YFrywgHqAYmKi6WnVPD4itHSiu3R-Q2cuBbyA3zy1yWCjVXc9j9iEDLAIMYAe4pg7QDmUUKA5ZaQxo/s1600/coolworld.in.png' width='32'/></a>
<a class='addthis_button_more'><img alt='More' height='32' src='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhegl2430zn0sMzduD4G1PfUXNLfbRybgZ8VavZ5Dukl2eTaR-glnxQ8MeiR4cBmtp4K9gwP4KTneXhqz1Y9DzuK5_IRiNgVxAE5CEpW8_ZCv0d4QsoGL9d_UpjWGF510eiEAVcDdxZ1M0/s1600/coolworld.in.png' width='32'/></a>
</div>
<script src='http://s7.addthis.com/js/250/addthis_widget.js' type='text/javascript'/>
</div>
<div style='clear:both;'/>
</script>
</div>
</div>
</div>
4) Customize your setting.Find any word click F3 or Ctrl+F.
  • Replace it USERNAME1 with your Feedburner Username.
  • Replace it USERNAME2 with your Twitter Username.
  • Replace it URL HERE with your Facebook Username.

No comments:

Post a Comment