Limit title length

It almost work correctly, but alwas return the same menu name:On home all menu elements name homeon contact all menu e

It almost work correctly, but alwas return the same menu name: On home all menu elements name home on contact all menu elements name contact

function max_title_length( $title ) {
    global $post;
    $id = ($post->ID);
    $title = get_post( $id )->post_title;
    $max = 20;
    if( strlen( $title ) > $max ) {
        return substr( $title, 0, $max ). " …";  
    }
    else {
        return $title;
    }
}
add_filter( 'the_title', 'max_title_length');

It almost work correctly, but alwas return the same menu name: On home all menu elements name home on contact all menu elements name contact

function max_title_length( $title ) {
    global $post;
    $id = ($post->ID);
    $title = get_post( $id )->post_title;
    $max = 20;
    if( strlen( $title ) > $max ) {
        return substr( $title, 0, $max ). " …";  
    }
    else {
        return $title;
    }
}
add_filter( 'the_title', 'max_title_length');
Share Improve this question edited Jul 25, 2019 at 14:05 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked Jul 25, 2019 at 13:58 nctincti 135 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 1

It's because you're not using the filter correctly. The the_title filter passes the title to be filtered as the $title argument, but you're overwriting it with this code:

global $post;
$id = ($post->ID);
$title = get_post( $id )->post_title;

That code's completely unnecessary because the title is already available in your function:

function max_title_length( $title ) {

So simply remove those lines to filter the correct title:

function max_title_length( $title ) {
    $max = 20;

    if ( strlen( $title ) > $max ) {
        return substr( $title, 0, $max ) . ' …';  
    } else {
        return $title;
    }
}
add_filter( 'the_title', 'max_title_length' );

Just be aware that — as you've experienced — the the_title filter applies to all titles, including posts, pages, and menu items. So if you only want your code to apply to titles output within The Loop, you can use the in_the_loop() function:

function max_title_length( $title ) {
    $max = 20;

    if ( in_the_loop() && strlen( $title ) > $max ) {
        return substr( $title, 0, $max ) . ' …';  
    } else {
        return $title;
    }
}
add_filter( 'the_title', 'max_title_length' );
function max_title_length($title) {
    if(is_front_page()){
        $max = 8;  
    }
    elseif(is_single()){
        $max = 5;
    }
    else{
        $max = 15;
    }


    if (strlen( $title ) > $max ) {
        return substr( $title, 0, $max ) . ' …';  
    } else {
        return $title;
    }
}
add_filter( 'the_title', 'max_title_length' );

it still cut the menu title. in_the_loop() function not working.

function max_title_length( $title ) {    
    $max = 20;
    if ( strlen( $title ) > $max ) {
       $title = substr( $title, 0, $max ) . ' …';  
    }
    return $title;
}
add_filter( 'the_title', 'max_title_length' );

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

相关推荐

  • Limit title length

    It almost work correctly, but alwas return the same menu name:On home all menu elements name homeon contact all menu e

    19小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信