Support » Fixing WordPress » trim title on an only single page

  • waseemaslam

    (@waseemaslam)


    I want to trim title on an only single page. (page-id : 3988)

    function limit_word_count($title) {
    $len = 5; //change this to the number of words
    if(!is_page(3988)){

    if (str_word_count($title) > $len) {
    $keys = array_keys(str_word_count($title, 2));
    $title = substr($title, 0, $keys[$len]);
    }

    return $title;
    }
    }
    add_filter(‘the_title’, ‘limit_word_count’);

    I’m trying this code. But it’s not working.

    Can anybody please help me with this.

    Regards,

    • This topic was modified 12 months ago by waseemaslam.
Viewing 11 replies - 1 through 11 (of 11 total)
  • Nikhil Bhansi

    (@nikhilbhansi)

    @waseemaslam
    Changed some lines from your code snippet.

    Updated Code Snippet:

    /**
     * Limit Post Title
     * https://wordpress.org/support/topic/trim-title-on-an-only-single-page/
     */ 
    function limit_word_count( $title, $post_id ) {
    	
    	$len = 5; //change this to the number of words
    	$trim_title_for_id = '3988';
    	
    	if( $post_id == $trim_title_for_id ){
    
    		if (str_word_count($title) > $len) {
    			$keys = array_keys(str_word_count($title, 2));
    			$title = substr($title, 0, $keys[$len]);
    		}
    
    	}
    	
    	return $title;
    }
    add_filter( 'the_title', 'limit_word_count', 10, 2 );
    waseemaslam

    (@waseemaslam)

    Thank you for the time but it’s not working

    https://i.imgur.com/KhqOe01.png

    I think code runs before and not getting the page id

    Nikhil Bhansi

    (@nikhilbhansi)

    Please make sure you’re providing correct post id.

    You can check the demo for the same here: http://clean-gerbil.w5.poopy.life/
    Also, you can play with this demo.
    I’ve trimmed title for Post ID 9.

    waseemaslam

    (@waseemaslam)

    So it’s little different on my site.

    I have a page with the post grid. There I want to display the title of 5 words.

    https://i.imgur.com/Vs5w5kn.png

    How can I do that on-page as is_page is not working?

    • This reply was modified 12 months ago by waseemaslam.
    Nikhil Bhansi

    (@nikhilbhansi)

    Can you please tell that ‘3988’ is the ID of the blog post or the page with post grid?

    waseemaslam

    (@waseemaslam)

    its the id of the page

    Nikhil Bhansi

    (@nikhilbhansi)

    Please pass the ID of the Blog post having title ‘Growing Connection Between…’.
    It will trim the title to five words as expected.

    waseemaslam

    (@waseemaslam)

    I tried already and it’s working fine but I want to trim title of all posts showing on that page and when a user visits single blog page I want to show full title.

    Nikhil Bhansi

    (@nikhilbhansi)

    Filter ‘the_title’ isn’t a good option for this.
    Please, apply some CSS for its class, it will solve your problem. 🙂

    .your-class-name-here {
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
    }

    You can set how many lines you want to show for that using webkit-line-clamp.

    Moderator Jan Dembowski

    (@jdembowski)

    Forum Moderator and Brute Squad

    @nikhilbhansi Please do not use short links in these forums. They get expanded when found and I have expanded yours.

    Nikhil Bhansi

    (@nikhilbhansi)

    @jdembowski noted 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘trim title on an only single page’ is closed to new replies.