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

Wednesday, June 15, 2011

Detect visitor browser within WordPress

How to detect the visitor browser within WordPress

To ensure maximum cross-browser compatibility on the wordpress blog theme, We have to able to detect the browser used by the blog visitor. WordPress can detect client browser. I describe the proces below.

Paste the code below in your functions.php file:


add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

if($is_lynx) $classes[] = 'lynx';
elseif($is_gecko) $classes[] = 'gecko';
elseif($is_opera) $classes[] = 'opera';
elseif($is_NS4) $classes[] = 'ns4';
elseif($is_safari) $classes[] = 'safari';
elseif($is_chrome) $classes[] = 'chrome';
elseif($is_IE) $classes[] = 'ie';
else $classes[] = 'unknown';

if($is_iphone) $classes[] = 'iphone';
return $classes;
}


Now saved the file and the function will automatically add a CSS class to the body tag, as shown in the exemple below:

body class="home blog logged-in safari"

So now with the help of the code, you just have to take your stylesheet, and add some browser-specific styles!

Tuesday, May 24, 2011

Display Twitter-like “time ago” on WP

Display Twitter-like “time ago” on your WordPress blog

Twitter have a built-in function that display time from now, like “3 days ago” or “more than a month ago”.If you want to do same with your WordPress post use the following function.

Paste the following into your functions.php file:






function post_time_ago( $type = 'post' ) {
    $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
    return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');
}

Now, you can use the following function in your theme files:
 echo post_time_ago();

Sunday, May 8, 2011

How to modify size of embedded videos

Modify size of embedded videos in wordpress

 In WordPress, it is easy to embed videos on the blog. But sometimes, dealing with  the video sizes is a bit painful. Here is a solution how you can adjust the size of any embedded video using a filter in wordpress

 Just  paste the following code on the functions.php file.

 function custom_embed_defaults($embed_size){
    $embed_size['width'] = 650; // Adjust values to your needs
    $embed_size['height'] = 550;

    return $embed_size;
 }

 add_filter('embed_defaults', 'custom_embed_defaults');

Saturday, April 23, 2011

Display wordpress post titles with a custom length

Display wordpress post titles with a custom length

Display wordpress post titles with a custom length

 If you want to  display only the first X characters of your wordpress post title, I display here some hack for that.

 The first thing to do is to create the function. Open your functions.php file and paste this code:

  function ODD_title($char) {
         $title = get_the_title($post->ID);
         $title = substr($title,0,$char);
         echo $title;
 }

 Now, you use the function on your wordpress theme files. Just pass how many characters you want to display as a parameter. In the following example, only the first 30 chars of the title will be displayed:

ODD_title(20);

So with the following code you can display wordpress post titles with a custom length

Set a maximum word count on post title

Set a maximum word count on wordpress post titles

 If you want to be able to set a maximum word count of post titles so you dont have to think titles that are too long, the following code is help you to overcome that. Here I’ll show you how you can easily set a maximum word count on post titles.


 To apply this hack on wordpress post title , just paste the following ode in your functions.php file:

 function maxWord($title){
    global $post;
    $title = $post->post_title;
    if (str_word_count($title) >= 10 ) //set this to the maximum number of words
        wp_die( __('Error: your post title is over the maximum word count.') );
 }
 add_action('publish_post', 'maxWord');

So with this code you can set a maximum word count on post title

Wednesday, March 30, 2011

How to remove WordPress admin bar from front end

In the following process you can  remove WordPress 3.1 admin bar

WordPress 3.1 introduced a new feature: the admin bar. It is very useful, but if you like to remove it. Please follow the instruction.

Paste the following piece of code into your functions.php file, save it to remove admin bar.

wp_deregister_script('admin-bar');
wp_deregister_style('admin-bar');
remove_action('wp_footer','wp_admin_bar_render',1000);

Thursday, March 24, 2011

How to replace words in your wordpress posts

Replace words in your wordpress posts

Let your Wordpress blog was named “portfolio” and you renamed it “myportfolio”. Then don’t edit your xxx posts to replace every single occurence! With this very useful code will do it for you, like a magic!


Just put words to replace in the array on line 4. Once done, paste the code into your function.php file, save the file, and you're ready!

function replace_text_wps($text){
   $replace = array(
       // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
       'portfolio' => 'myportfolio',
       'excerpt' => 'excerpt',
       'function' => 'function'
   );
   $text = str_replace(array_keys($
replace), $replace, $text);
   return $text;
}

add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');

Tuesday, February 1, 2011

How to increase WordPress memory limit

If ever this happen “Allowed memory size of xxx bytes exhausted”, then we need to increase WordPress memory limit. This can be done


open your wp-config file and paste the following code in it.

define('WP_MEMORY_LIMIT', '96M');

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