• 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 5 years, 7 months ago by waseemaslam.
Viewing 11 replies - 1 through 11 (of 11 total)
  • @waseemaslam
    Changed some lines from your code snippet.

    Updated Code Snippet:

    /**
     * Limit Post Title
     * https://www.ads-software.com/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 );
    Thread Starter waseemaslam

    (@waseemaslam)

    Thank you for the time but it’s not working

    View post on imgur.com

    I think code runs before and not getting the page id

    Please make sure you’re providing correct post id.

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

    • This reply was modified 5 years, 7 months ago by Jan Dembowski.
    Thread Starter 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.

    View post on imgur.com

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

    • This reply was modified 5 years, 7 months ago by waseemaslam.

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

    Thread Starter waseemaslam

    (@waseemaslam)

    its the id of the page

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

    Thread Starter 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.

    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.

    @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.