Monday, January 16, 2012

Display custom text in wordpress

Display custom text in wordpress

Using modified the_excerpt() function we can display an excerpt which is not longer than a predetermined length and doesn’t cut off in mid-sentence.

To do is open functions.php file and paste the code below in it which works as a function

// Variable & intelligent excerpt length.
function print_excerpt($length) { // Max excerpt length. Length is set in characters
    global $post;
    $text = $post->post_excerpt;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
    }
    $text = strip_shortcodes($text); // optional, recommended
    $text = strip_tags($text); // use ' $text = strip_tags($text,'< p >< a >'); ' if you want to keep some tags

    $text = substr($text,0,$length);
    $excerpt = reverse_strrchr($text, '.', 1);
    if( $excerpt ) {
        echo apply_filters('the_excerpt',$excerpt);
    } else {
        echo apply_filters('the_excerpt',$text);
    }
}

// Returns the portion of haystack which goes until the last occurrence of needle
function reverse_strrchr($haystack, $needle, $trail) {
    return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}



Now you can use the print_excerpt() function in the theme files like this:

 print_excerpt(50);

Wednesday, December 28, 2011

Display a thumbnail from a Youtube using a shortcode

 How to display a thumbnail from a Youtube using a shortcode

Copy the following code below and then paste it into the functions.php file under theme folder.

/*
    THis Shortcode will display youtube thumbnail on the wordpress blog.
    Usage:
    [youtube_thumb id="VIDEO_ID" img="0" align="left"]
    VIDEO_ID= Youtube video id
    img=0,1,2 or 3
    align= left,right,center
*/
function wp_youtube_video_thumbnail($atts) {
     extract(shortcode_atts(array(
          'id' => '',
          'img' => '0',
          'align'=>'left'
     ), $atts));
    $align_class='align'.$align;
   
}
add_shortcode('youtube_thumb', 'wp_youtube_video_thumbnail');

Now, you can use the shortcode. The shortcode accept 3 parameters: The video ID, the image size (0 for 480*360px, 1 for 120*90) and the image alignment.

[youtube_thumb id="8O9YA7PZZe0" img="0" align="center"]

Wednesday, November 30, 2011

Automatically remove p tags on images in wordpress

Automatically remove p tags on images in wordpress
Wordpress By default embed images in < p > tags. but if you don’t want those p tags to show, the following example display a great way to remove the < p > tags without having to chnage the WordPress core

Just paste the following code in theme functions.php file, and save the file.the
tags are gone now.





add_filter('the_content', 'remove_ptags_on_images');

Monday, October 31, 2011

Login form shortcode for WordPress

Here we learn how to displaying login forms on wordpress using a shortcode Paste the following code into functions.php file:

function sandip_login_form_shortcode() {
    if ( is_user_logged_in() )
        return '';

    return wp_login_form( array( 'echo' => false ) );
}

function sandip_add_shortcodes() {
    add_shortcode( 'sandip-login-form', 'sandip_login_form_shortcode' );
}

add_action( 'init', 'sandip_add_shortcodes' );

Once done, we can now use the shortcode as shown in the following example. just paste it on the post editor, where you want the login form to be displayed. [sandip-login-form]

Friday, September 30, 2011

Retrieve a remote page using WordPress

How to retrieve a remote page using WordPress


If you want to get the content,file info from a remote file for use it in wordpress instal then follow the step describe below.
Use the wp_remote_get() function (Or wp_remote_post() is you prefer using the POST method) to retrieve the desired url. The following example describe how to retrieve an url and display its content as well as the file info.

This code can be used anywhere on your template files.

$response = wp_remote_get( 'http://sandipdas.in/file.txt' );
if( is_wp_error( $response ) ) {
echo 'Something went wrong!';
} else {
echo 'Response:';
print_r( $response );

';
}

Wednesday, September 28, 2011

Display your latest Google+ update on WordPress

How to display your latest Google+ update on your WordPress blog


Just paste the following code where you want to display your latest Google+ update. Don't forget to put your Google+ ID on line 3.


include_once(ABSPATH.WPINC.'/rss.php');
$googleplus = fetch_feed("http://plusfeed.appspot.com/117307278436515842100

"); // Replace 117307278436515842100 by your own ID
echo '< a href="'; echo $googleplus->items[0]['link']; echo '">';
echo $googleplus->items[0]['summary'];


echo '';

Wednesday, August 24, 2011

Display a thumbnail of any website in wordpress

In WordPress we can display a thumbnail of any website. Wordpress have a service named mShots, Its allow us to get snapshots of any website. We can use a shotcode to easily display a snapshot of a given website.


To create the shortcode ,just paste the following code into functions.php file.



function wpr_snap($atts, $content = null) {
extract(shortcode_atts(array(
"snap" => 'http://s.wordpress.com/mshots/v1/',
"url" => 'http://www.sandipdas.in',
"alt" => 'My image',
"w" => '500', // width
"h" => '400' // height
), $atts));

$img = '' . $alt . '';
return $img;
}

add_shortcode("snap", "wpr_snap");


Now use the following shortcode to display any snaphot

[snap url="http://www.sandipdas.in" alt="description" w="500" h="400"]

Saturday, July 23, 2011

Display Custom Fields Outside Loop in WordPress

How to Display Custom Fields Outside The Loop in WordPress

When you need to display post-specific content for example a data for a specific post. Usually post specific contents are added via a custom field which can only be displayed inside a post loop.Here we will show you how you can display custom fields outside the loop.

Please check the following code:


global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field-data', true);
wp_reset_query();


You will need to make sure that you change the part where it says “Your-Custom-Field-data”.

Tuesday, July 5, 2011

Wordpress Tricks

Get Category Name By ID (WordPress)
eho get_cat_name( $cat_id );


Get Category Link By ID (WordPress)
echo get_category_link($cat_id);

Sunday, June 26, 2011

A Custom Read More in Wordpress

Display A Custom Read More in Wordpress

To display "A Custom Read More" all you need to do is replace your usual the_content template tag with the following code. So when you write a new post, create a new custom field with the key name "custom_more".

$custommore = get_post_meta($post->ID, 'custom_more', true);
if (!$custommore) { $custommore = 'Read More »'; }
the_content($custommore);

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off