Set max title length - similar to excerpt length for outside of single.php

I've been searching for a code snippet that lets me limit the title length similar to what you can to with the_exce

I've been searching for a code snippet that lets me limit the title length similar to what you can to with the_excerpt and then just display "(...)" if the title is too long. I'd like to use that in loops outside of single.php, e.g. in my sidebar where an extremely long title would harm the layout.

All I could find was this code snippet, but that does obviously not what I want.

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

Is it possible at all?

I'm looking for something similar like this, just not for the excerpt, but the title:

function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).' (...)';
      } else {
        $excerpt = implode(" ",$excerpt);
      } 
      $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
      return $excerpt;
    }

    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).' (...)';
      } else {
        $content = implode(" ",$content);
      } 
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]>', $content);
      return $content;
    }

I've been searching for a code snippet that lets me limit the title length similar to what you can to with the_excerpt and then just display "(...)" if the title is too long. I'd like to use that in loops outside of single.php, e.g. in my sidebar where an extremely long title would harm the layout.

All I could find was this code snippet, but that does obviously not what I want.

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

Is it possible at all?

I'm looking for something similar like this, just not for the excerpt, but the title:

function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).' (...)';
      } else {
        $excerpt = implode(" ",$excerpt);
      } 
      $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
      return $excerpt;
    }

    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).' (...)';
      } else {
        $content = implode(" ",$content);
      } 
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]>', $content);
      return $content;
    }
Share Improve this question edited Oct 3, 2011 at 1:32 japanworm asked Oct 2, 2011 at 12:42 japanwormjapanworm 5873 gold badges19 silver badges40 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

This depends 100% on how you're getting the title. If you're using a global object (i.e. $post->post_title then you're not passing it through any filters and you'll have to use some fancy post-processing to pare the title down.

However, if you're inside a post loop, use either the_title() to echo the current post's title or get_the_title() to return it programatically.

If you use one of those two functions, WordPress will automatically pass the post title through a filter before giving it back to you.

Then you can add the following to the top of your sidebar.php file:

function japanworm_shorten_title( $title ) {
    $newTitle = substr( $title, 0, 20 ); // Only take the first 20 characters
    
    return $newTitle . " …"; // Append the elipsis to the text (...) 
}
add_filter( 'the_title', 'japanworm_shorten_title', 10, 1 );

Now, every time you reference the_title() or get_the_title() in your sidebar, they will return the automatically-shortened version instead of the full version.

Just remember to remove this filter at the end of your sidebar.php file or it will apply elsewhere in your theme as well:

remove_filter( 'the_title', 'japanworm_shorten_title' );

Update 10/3/2011

If you want a function you can use anywhere, I recommend making your own versions of get_the_title() and the_title() and using them in your code. For example:

function japanworm_get_the_title( $length = null, $id = 0 ) {
    $post = &get_post($id);

    $title = isset($post->post_title) ? $post->post_title : '';
    $id = isset($post->ID) ? $post->ID : (int) $id;

    if ( !is_admin() ) {
        if ( !empty($post->post_password) ) {
            $protected_title_format = apply_filters('protected_title_format', __('Protected: %s'));
            $title = sprintf($protected_title_format, $title);
        } else if ( isset($post->post_status) && 'private' == $post->post_status ) {
            $private_title_format = apply_filters('private_title_format', __('Private: %s'));
            $title = sprintf($private_title_format, $title);
        }
    }

    // Shorten the title
    if ( null != $length ) {
        $length = (int) $length;

        $title = substr( $title, 0, $length ); // Only take the first 20 characters
    
        $title .= " …";
    }

    return apply_filters( 'the_title', $title, $id );
}

function japanworm_the_title($before = '', $after = '', $echo = true, $length = null) {
    $title = get_the_title($length);

    if ( strlen($title) == 0 )
        return;

    $title = $before . $title . $after;

    if ( $echo )
        echo $title;
    else
        return $title;
}

These are copied from the original the_title() and get_the_title() functions, so they should work in the loop the same way. I haven't tested this, though.

You can use the wp_trim_excerpt() function.

If you want need to specify chars limit, you should be able to use the excerpt_length filter.

<?php echo wp_trim_excerpt( get_the_title() ); ?>

Wordpress API Solution

If you want to trim english only or character only language, using substring would be the right way. However, if you want to trim language like Chinese, Japanese or the others, using substring might be awful, since they are using unicode to represent.

Using wp_trim_words() would be a simple and direct way to solve your problem.

wp_trim_words( $text, $num_words = 55, $more = null );

p.s. inspired by Leo Caseiro.

CSS Only Solution

If you want to find out how to trim the words dynamically especially in responsive design, you should use CSS like this:

.card-title {
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745088447a4610545.html

相关推荐

  • Set max title length - similar to excerpt length for outside of single.php

    I've been searching for a code snippet that lets me limit the title length similar to what you can to with the_exce

    15小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信