Snippets

1.

Popular Posts

2.

    3. get_results(“SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5”);
    4. foreach ($result as $post) {
    5. setup_postdata($post);
    6. $postid = $post->ID;
    7. $title = $post->post_title;
    8. $commentcount = $post->comment_count;
    9. if ($commentcount != 0) { ?>
    10.

  • <a href="%3C?php%20echo%20get_permalink%28$postid%29;%20?%3E" title="”>
    11. {}
  • 12.
    13.
    14.

——————————————–
1. ID);
4. if ($tags) {
5. echo ‘Related Posts’;
6. $first_tag = $tags[0]->term_id;
7. $args=array(
8. ‘tag__in’ => array($first_tag),
9. ‘post__not_in’ => array($post->ID),
10. ‘showposts’=>5,
11. ‘caller_get_posts’=>1
12. );
13. $my_query = new WP_Query($args);
14. if( $my_query->have_posts() ) {
15. while ($my_query->have_posts()) : $my_query->the_post(); ?>
16.
17. <a href="%3C?php%20the_permalink%28%29%20?%3E" rel="bookmark" title="Permanent Link to “>
18.
19.

——————————

1. function catch_that_image() {
2. global $post, $posts;
3. $first_img = ”;
4. ob_start();
5. ob_end_clean();
6. $output = preg_match_all(‘//i’, $post->post_content, $matches);
7. $first_img = $matches [1] [0];
8.
9. if(empty($first_img)){ //Defines a default image
10. $first_img = “/images/default.jpg”;
11. }
12. return $first_img;
13. }
—————————

1. function catch_that_image() {
2. global $post, $posts;
3. $first_img = ”;
4. ob_start();
5. ob_end_clean();
6. $output = preg_match_all(‘//i’, $post->post_content, $matches);
7. $first_img = $matches [1] [0];
8.
9. if(empty($first_img)){ //Defines a default image
10. $first_img = “/images/default.jpg”;
11. }
12. return $first_img;
13. }

———————–

returns all images from a post

1.
2.
3.
4. post_content;
6. $szSearchPattern = ‘~]*\ />~’;
7.
8. // Run preg_match_all to grab all the images and save the results in $aPics
9. preg_match_all( $szSearchPattern, $szPostContent, $aPics );
10.
11. // Check to see if we have at least 1 image
12. $iNumberOfPics = count($aPics[0]);
13.
14. if ( $iNumberOfPics > 0 ) {
15. // Now here you would do whatever you need to do with the images
16. // For this example the images are just displayed
17. for ( $i=0; $i
———————————

First open the “single.php” in your theme folder and save it as “image.php” in same folder. Now open “image.php” and search the line which displays the post content. It should be somewhat in the following form. It can differ a bit but the function is called by the_content like this
view plaincopy to clipboardprint?

1.

Now insert the following code above the aforementioned code (the_content).

1.

2. ID, ‘medium’ ); ?>
3.
4.

5. post_excerpt) ) the_excerpt(); // this is the “caption” ?>
6.

Also insert the following code below the aforementioned code (the_content).

1.

2.

3.

4.

5.
6.

Now, add this CSS to your theme’s style.css file
view plaincopy to clipboardprint?

1. /****************Image Gallery *********************/
2. .gallery {text-align:center;}
3. .gallery img {padding:2px; height:100px; width:100px;}
4. .gallery a:hover {background-color:#ffffff;}
5. .attachment {text-align:center;}
6. .attachment img { padding:2px; border:1px solid #999999;}
7. .attachment a:hover {background-color:#FFFFFF;}
8. .imgnav {text-align:center;}
9. .imgleft {float:left;}
10. .imgleft a:hover {background-color:#FFFFFF;}
11. .imgleft img{ padding:2px; border:1px solid #999999; height:100px; width:100px;}
12. .imgright {float:right;}
13. .imgright a:hover {background-color:#FFFFFF;}
14. .imgright img{ padding:2px; border:1px solid #999999; height:100px; width:100px;}

Now to use this gallery, just upload your images in a post or a page, go to the gallery option (after you have finished uploading all your images) and insert gallery into your post/page

TWITTER

1. Share on Twitter
————————-

1. //get cool feedburner count
2. $whaturl=”http://api.feedburner.com/awareness/1.0/GetFeedData?uri=feedburner-id&#8221;;
3.
4. //Initialize the Curl session
5. $ch = curl_init();
6.
7. //Set curl to return the data instead of printing it to the browser.
8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
9.
10. //Set the URL
11. curl_setopt($ch, CURLOPT_URL, $whaturl);
12.
13. //Execute the fetch
14. $data = curl_exec($ch);
15.
16. //Close the connection
17. curl_close($ch);
18. $xml = new SimpleXMLElement($data);
19. $fb = $xml->feed->entry[‘circulation’];
20. //end get cool feedburner count

Now paste this anywhere you want and it’ll display a Feedburner subscriber count in text.

————————-
1. function publish_later_on_feed($where) {
2. global $wpdb;
3.
4. if ( is_feed() ) {
5. // timestamp in WP-format
6. $now = gmdate(‘Y-m-d H:i:s’);
7.
8. // value for wait; + device
9. $wait = ‘5’; // integer
10.
11. // http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
12. $device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
13.
14. // add SQL-sytax to default $where
15. $where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
16. }
17. return $where;
18. }
19.
20. add_filter(‘posts_where’, ‘publish_later_on_feed’);

———————
How to Exclude Categories from Your RSS Feed

1. function myFilter($query) {
2. if ($query->is_feed) {
3. $query->set(‘cat’,’-5′); //Don’t forget to change the category ID =^o^=
4. }
5. return $query;
6. }
7.
8. add_filter(‘pre_get_posts’,’myFilter’);
————————-

TWITTER HACKS

1. <a href="http://twitter.com/home?status=Currently reading ” title=”Send this page to Twitter!” target=”_blank”>Spread on Twitter

1. function getTinyUrl($url) {
2. $tinyurl = file_get_contents(“http://tinyurl.com/api-create.php?url=&#8221;.$url);
3. return $tinyurl;
4. }

1. ID));
3. echo ‘Tiny Url for this post: ‘.$turl.’
4. ?>

——————-

1. function string_getInsertedString($long_string,$short_string,$is_html=false){
2. if($short_string>=strlen($long_string))return false;
3. $insertion_length=strlen($long_string)-strlen($short_string);
4. for($i=0;$i<strlen ($short_string);++$i){
5. if($long_string[$i]!=$short_string[$i])break;
6. }
7. $inserted_string=substr($long_string,$i,$insertion_length);
8. if($is_html && $inserted_string[$insertion_length-1]=='<'){
9. $inserted_string='saveHTML();
16. $element->parentNode->removeChild($element);
17. $html2=$document->saveHTML();
18. return string_getInsertedString($html,$html2,true);
19. }
20.
21. function getFollowers($username){
22. $x = file_get_contents(“http://twitter.com/&#8221;.$username);
23. $doc = new DomDocument;
24. @$doc->loadHTML($x);
25. $ele = $doc->getElementById(‘follower_count’);
26. $innerHTML=preg_replace(‘/^]*>(.*)]*>$/’,”\\1″,DOMElement_getOuterHTML($doc,$ele));
27. return $innerHTML;
28. }

1.
——————————–

allows us to pull the latest tweet from a Twitter user via the RSS feed that is produced by Twitter.

1. Twitter
————————–
1. <?php
2. $username = "TwitterUsername"; // Your twitter username.
3. $prefix = ""; // Prefix – some text you want displayed before your latest tweet.
4. $suffix = ""; // Suffix – some text you want display after your latest tweet.
5. $feed = "http://search.twitter.com/search.atom?q=from:&quot; . $username . "&rpp=1";
6.
7. function parse_feed($feed) {
8. $stepOne = explode("”, $feed);
9. $stepTwo = explode(“”, $stepOne[1]);
10. $tweet = $stepTwo[0];
11. $tweet = str_replace(“<", "”, “>”, $tweet);
13. return $tweet;
14. }
15.
16. $twitterFeed = file_get_contents($feed);
17. echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix);
18. ?>

——————-

pull multiple tweets from multiple user accounts and display them

1. <?php
2. $usernames = "Username Username Username"; // Pull from accounts, separated by a space
3. $limit = "5"; // Number of tweets to pull in, total.
4. $show = 1; // Show username? 0 = No, 1 = Yes.
5.
6. $prefix = ""; // This comes before the entire block of tweets.
7. $prefix_sub = ""; // This comes before each tweet on the feed.
8. $wedge = ""; // This comes after the username but before the tweet content.
9. $suffix_sub = "
"; // This comes after each tweet on the feed.
10. $suffix = ""; // This comes after the entire block of tweets.
11.
12. function parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub) {
13.
14. $usernames = str_replace(" ", "+OR+from%3A", $usernames);
15. $feed = "http://search.twitter.com/search.atom?q=from%3A&quot; . $usernames . "&rpp=" . $limit;
16. $feed = file_get_contents($feed);
17. $feed = str_replace("&", "&", $feed);
18. $feed = str_replace("<", "”, “>”, $feed);
20. $clean = explode(“”, $feed);
21. $amount = count($clean) – 1;
22.
23. for ($i = 1; $i <= $amount; $i++) {
24.
25. $entry_close = explode("”, $clean[$i]);
26. $clean_content_1 = explode(“”, $entry_close[0]);
27. $clean_content = explode(“”, $clean_content_1[1]);
28. $clean_name_2 = explode(“”, $entry_close[0]);
29. $clean_name_1 = explode(“(“, $clean_name_2[1]);
30. $clean_name = explode(“)”, $clean_name_1[1]);
31. $clean_uri_1 = explode(“”, $entry_close[0]);
32. $clean_uri = explode(“”, $clean_uri_1[1]);
33.
34. echo $prefix_sub;
35. if ($show == 1) { echo “” . $clean_name[0] . “” . $wedge; }
36. echo $clean_content[0];
37. echo $suffix_sub;
38.
39. }
40. }
41. echo $prefix;
42. parse_feed($usernames, $limit, $show, $prefix_sub, $wedge, $suffix_sub);
43. echo $suffix;
44. ?>

———————–
1.

———————–
1.

———————

1.

———————–

Advertisements

~ by netlex on October 31, 2009.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: