0

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');
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' );
| improve this answer | |
  • With in_the_loop() function, it limit just title from ads(using classipress), rest posts and custom posts not working. – ncti Jul 26 '19 at 7:27
  • It sounds like you might be doing something weird with your loop then. Remove it if it's causing problems, but I can't offer any more advice without seeing the relevant template. – Jacob Peattie Jul 26 '19 at 16:09
  • in classipress theme ads it's like post, and this loop working, but working just for ads, not working for posts. – ncti Jul 29 '19 at 11:48
0
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.

| improve this answer | |
-1
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' );
| improve this answer | |
  • Welcome to WPSE! While your answer looks like it's technically correct and should work for the OP, it's short on details and explanation. Please see How do I write a good answer? in the site's Help section. – Pat J Jul 26 '19 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.