How To Limit WordPress the_content By Words
With the help of snippet below, you will be able to limit WordPress the_content by words.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
//Add this function to your functions.php file function get_news($cat_slug, $limit){ $my_query = new WP_Query('category_name='.$cat_slug.'&posts_per_page='.$limit); $result = "<ul class='news-feed'>"; while ($my_query->have_posts()) : $my_query->the_post(); $result .= '<li class="news-item"><a href="' . get_permalink() . '" target="_blank">' . get_the_title() . '</a><p>'; $result .= apply_filters( 'the_content', wp_trim_words( get_the_content(), 15, '…' ) ) . ' <a href="' . get_permalink() . '" target="_blank" class="read_more"><strong>read more</strong> »</a>'; $result .= '</li>'; endwhile; $result .= '</ul>'; return $result; } //Usage: // get company news from the 'news' category with limit of 2 posts print get_news('news', 2); |
Snippet Source/Credit: StackExchange
How To List Author Comments On Author Page
Using the following snippet, you will be able to list all the author comments on authors page.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
function wpse19316_author_comments( $length ) { $final_length = (int) $length; $author_comments = get_comments( array( 'ID' => $GLOBALS['authordata']->ID ) ); foreach ( $author_comments as $comment ) { $comment_length = sublen( $comment->comment_content ); $comment_excerpt = $comment->comment_content; if ( $comment_length > $final_length ) $comment_excerpt = substr( $comment->comment_content, $final_length ); echo $comment_excerpt.'<br />'; } } |
Snippet Source/Credit: StachExchange
How To No-Follow WordPress Tag Cloud
Simply add the snippet to your theme’s functions.php file and you will be able to no-follow WordPress tag cloud in your website.
1 2 3 4 5 6 7 |
function add_nofollow_tag($sLink) { return str_replace('<a href=', '<a rel="nofollow" href=', $sLink); } add_filter('wp_tag_cloud', 'add_nofollow_tag'); |
Snippet Source/Credit: Snipplr
How To Paginate WordPress Posts
Adding the following snippet to your theme’s functions.php file, you will be able to paginate WordPress posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
<?php function wp_paginate($pgs = '', $rng = 2) { $itm = ($rng * 2) + 1; global $pgd; if (empty($pgd)) $pgd = 1; if ($pgs == '') { global $wp_query; $pgs = $wp_query->max_num_pgs; if (!$pgs) { $pgs = 1; } } if (1 != $pgs) { // echo the HTML output echo "<div class='wp_paginate'>"; if ($pgd > 2 && $pgd > $rng + 1 && $itm < $pgs) echo "<a href='" . get_pagenum_link(1) . "'>«</a>"; if ($pgd > 1 && $itm < $pgs) echo "<a href='" . get_pagenum_link($pgd - 1) . "'>‹</a>"; for ($i = 1; $i <= $pgs; $i++) { if (1 != $pgs && (!($i >= $pgd + $rng + 1 || $i <= $pgd - $rng - 1) || $pgs <= $itm)) { echo ($pgd == $i) ? "<span class='current'>" . $i . "</span>" : "<a href='" . get_pagenum_link($i) . "' class='inactive' >" . $i . "</a>"; } } if ($pgd < $pgs && $itm < $pgs) echo "<a href='" . get_pagenum_link($pgd + 1) . "'>›</a>"; if ($pgd < $pgs - 1 && $pgd + $rng - 1 < $pgs && $itm < $pgs) echo "<a href='" . get_pagenum_link($pgs) . "'>»</a>"; echo "</div>\n"; } } ?> |
Snippet Source/Credit: Design-Ninja
How To Remove WordPress Version Meta
Using the following snippet in your theme’s functions.php file, you will be able to remove WordPress version meta.
1 |
remove_action('wp_head', 'wp_generator'); |
How To Render Your Twitter Followers Count In Your WordPress Blog
Adding the following snippet to your theme’s functions.php file, you will be able to render your Twitter followers count. Also, remember to replace “planetabhi” with your own Twitter screen name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
function my_followers_count($screen_name = 'planetabhi'){ $key = 'my_followers_count_' . $screen_name; // Let's see if we have a cached version $followers_count = get_transient($key); if ($followers_count !== false) return $followers_count; else { // If there's no cached version we ask Twitter $response = wp_remote_get("http://api.twitter.com/1/users/show.json?screen_name={$screen_name}"); if (is_wp_error($response)) { // In case Twitter is down we return the last successful count return get_option($key); } else { // If everything's okay, parse the body and json_decode it $json = json_decode(wp_remote_retrieve_body($response)); $count = $json->followers_count; // Store the result in a transient, expires after 1 day // Also store it as the last successful using update_option set_transient($key, $count, 60*60*24); update_option($key, $count); return $count; } } } echo "I have " . my_followers_count('planetabhi') . " followers"; |
Snippet Source/Credit: Snipplr