Waseem Aslam
Waseem Aslam

Reputation: 3

Trim title on an only single page wordpress

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

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

Can anybody please help me with this.

Regards,

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

In result, this code removes the title from the page

Upvotes: 0

Views: 638

Answers (1)

mahmood beheshti
mahmood beheshti

Reputation: 69

There are many ways to do this.

1 -- you can use mb_strimwidth function for example :

echo mb_strimwidth(get_the_title(), 0, 50, '...');

2 -- and if you don't want use php simply with css can do this.with this code you can Limit titles to one line only. just replace title class.

.single .titleclass {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
} 

3 -- also you can use wp_trim_words() function for wordpress.

wp_trim_words( string $text, int $num_words = 55, string $more = null )

for example :

<?php echo wp_trim_words( get_the_title(), 15 ); ?>

4-- use this function :

function limit_word_count($title) {

        // limit to 5 words
        return wp_trim_words( $title, 5, '' );

    }

add_filter('the_title', 'limit_word_count');

Upvotes: 1

Related Questions