PUSTAKAKORAN.COM
PUSTAKAKORAN.COM

Reputation: 477

How to limit the title of wordpress to specific categories?

How to limit the title of wordpress for a particular category

I have this code to general limit the title of wordpress:

code in functions.php:

function titleLimitChar($title){
        if(strlen($title) > 30 && !(is_single()) && !(is_page())){ // not in single post page and page.
            $title = substr($title,0,30) . "..";
        }
        return $title;
    }
    add_filter( 'the_title', 'titleLimitChar' );

What should I add to the code to limit the title of wordpress to specific categories?

Upvotes: 0

Views: 236

Answers (2)

Toriqul Islam Tareq
Toriqul Islam Tareq

Reputation: 472

Your code is fine for common title limitation. You have to add extra checking for your category in the method as

function titleLimitChar($title){
        if(is_category( '9' )){
            if(strlen($title) > 30 && !(is_single()) && !(is_page())){ // not in single post page and page.
                $title = substr($title,0,30) . "..";
            }
             return $title;
        }else{
         return $title;
        }   


    }
    add_filter( 'the_title', 'titleLimitChar' );

in the is_category() you use category id, category name and catgory-slug etc.

For Multiple categories just pass array in the parameter as

array( 9, 'blue-cheese', 'Stinky Cheeses' )

Other uses to check specific category or categories as below from the WordPress developer portal.

is_category();
// When any Category archive page is being displayed.

is_category( '9' );
// When the archive page for Category 9 is being displayed.

is_category( 'Stinky Cheeses' );
// When the archive page for the Category with Name "Stinky Cheeses" is being displayed.

is_category( 'blue-cheese' );
// When the archive page for the Category with Category Slug "blue-cheese" is being displayed.

is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) );

Source : Wp Developer is_category() function ;

Thank you!

Upvotes: 1

Purvik Dhorajiya
Purvik Dhorajiya

Reputation: 4870

you can use wp_trim_words()

This function trims text to a certain number of words and returns the trimmed text.

echo wp_trim_words( get_the_title(), 40, '...' );

Upvotes: 1

Related Questions