Post Views Snippets

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);
    }
}

Paste the below code in the single page, so that it count your views.

setPostViews(get_the_ID());
Advertisements

Display External RSS Feeds in our Site

Here is the code for displaying External RSS Feeds in our Worpdress Site.

<?php include_once(ABSPATH.WPINC.'/feed.php');
$rss = fetch_feed('Feed URL Goes Here');
$maxitems = $rss->get_item_quantity(4);
$rss_items = $rss->get_items(0, $maxitems);
?>
<ul style="font-family:Helvetica;font-size:15px;list-style:square;">
<?php if ($maxitems == 0) echo '<li>No items.</li>';
else
// Loop through each feed item and display each item as a hyperlink.
foreach ( $rss_items as $item ) : ?>
<li>
<a href='<?php echo $item->get_permalink(); ?>'
title='<?php echo 'Posted '.$item->get_date('j F Y | g:i a'); ?>' target="_blank">
<?php echo $item->get_title(); ?></a>
</li>
<?php endforeach; ?>
</ul>

Keep Spreading…

Display the number of your Twitter Followers

Here is the code for display the number of followers who are following you on twitter.

Copy and Paste the below code in ‘functions.php’ file.

function string_getInsertedString($long_string,$short_string,$is_html=false){
  if($short_string>=strlen($long_string))return false;
  $insertion_length=strlen($long_string)-strlen($short_string);
  for($i=0;$i<strlen($short_string);++$i){
    if($long_string[$i]!=$short_string[$i])break;
  }
  $inserted_string=substr($long_string,$i,$insertion_length);
  if($is_html && $inserted_string[$insertion_length-1]=='<'){
    $inserted_string='<'.substr($inserted_string,0,$insertion_length-1);
  }
  return $inserted_string;
}

function DOMElement_getOuterHTML($document,$element){
  $html=$document->saveHTML();
  $element->parentNode->removeChild($element);
  $html2=$document->saveHTML();
  return string_getInsertedString($html,$html2,true);
}

function getFollowers($username){
  $x = file_get_contents("http://twitter.com/".$username);
  $doc = new DomDocument;
  @$doc->loadHTML($x);
  $ele = $doc->getElementById('follower_count');
  $innerHTML=preg_replace('/^<[^>]*>(.*)<[^>]*>$/',"\\1",DOMElement_getOuterHTML($doc,$ele));
  return $innerHTML;
}

And then copy and paste the below code to display the number of followers count.

<?php echo getFollowers("your twitter username")." Followers"; ?>

Keep Sharing and njoy.

Display WordPress comments in wordpress

I was trying to figure out, how i can display wordpress comment with a help of little coding..

<div class="post-recent-comment" style="font-size: 13px; font-weight: bold; font-family: Arial,helvetica;>

<?php


$comment_array = array_reverse(get_approved_comments($wp_query->post->ID));


$count = 1;


 ?>


<?php if ($comment_array) { ?>


<span class="comment"><?php comments_number('No comment','1 comment','% comments'); ?></span> – Latest by:


<ul class="commentlist"”>


<?php foreach($comment_array as $comment){ ?>


<?php if ($count++ <= 10) { ?>


<li><?php echo($comment->comment_author . '<br />' . $comment->comment_content); ?></li>


<?php } ?>


<?php } ?>


</ul>


<?php } else { ?>


<span class="comment">No comments so far</span>


<?php } ?>


</div> 

Seperate a String variable with the delimiters

I have been trying to fix a gallery of images in my project. Atlast i have landed up with a solution we can get the collection of a images Url seperated with commas and then make it stored in the array variable and retrieve it back.

<?php 
$image=get_post_meta($post->ID, 'custom field name', true);
$img=explode(',',$image);
for($i=0;$i<sizeof($img);$i++)
{
?>
<a href="<?php if((($img[$i] == substr($img[$i],0,57).''.'.jpg'))) { echo substr($img[$i],0,57).''.'_b.jpg'; } else { echo substr($img[$i],0,57).''.'_b.jpg'; } ?>"><img src="<?php if((($img[$i] == substr($img[$i],0,57).''.'.jpg'))) { echo substr($img[$i],0,57).''.'_m.jpg'; } else { echo substr($img[$i],0,57).''.'_m.jpg'; } ?>" style="width:172px;"/></a>
<?php
}
?>

Thanks for my dream one.