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

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