How to limit the word count in post title?
Sometimes in theme development, we have to reduce the size of the blog headline to keep it inline or keep it in a grid or same height, we had to keep the blog title in fixed words. so long blog post titles take a certain size of the page. and don’t get oversized.
There are two methods to do that, if you are an expert in PHP coding you can do it with only one PHP function, if you don’t have any experience in coding, you can do it using the plugin.
Method 1: wordpress post title limit characters by coding
if you are a PHP expert, there you have to do a tricky task. you have to find the_title() function in theme files, where you want to limit post title length, replace it with this function
echo wp_trim_words( get_the_title(), 10, '...' );
or if this isn’t possible to replace all the_title() functions, there is another solution for it, you can do it by
adding a hook filter to the_title() function. check out the code below
add_filter( 'the_title', 'the_dailywp_trim_words' ); function the_dailywp_trim_words( $title ) { // limit to ten words return wp_trim_words( $title, 10, '' ); }
if you need a more advanced level like if you want to check if it’s not a blog post, you won’t limit post title characters. you can do it by the below code example, with the below code even you can limit the post title by checking its post id.
add_filter( 'the_title', 'the_dailywp_trim_words_by_post_type', 10, 2 ); function the_dailywp_trim_words_by_post_type( $title, $post_id ) { $post_type = get_post_type( $post_id ); if ( 'product' !== $post_type ) return $title; // limit to ten words return wp_trim_words( $title, 10, '' ); }
you can replace ‘product’ with your post type name. Please replace 10 with your character’s number. check out more about wp_trim_words functions on the wordpress.org website.
Method 2: Limit post title length wordpress plugin
You can download the plugin from the WordPress plugin directory for free, just install it as any other plugin, after installation, there is an option in this plugin, Go to the Setting > Limit Post Titles option.
you can set characters by Limit post titles plugin options and you can select post type. if you don’t want to limit characters on the Page, you can just uncheck it. and save it.
WooCommerce Solutions
in case you have to limit your wooCommerce product title characters, there is a plugin for only Woocommerce, as WooCommerce has a different page, Like a Product single page, Product category archive page, Shop page, or product on the Home page. if this plugin you can set different characters limit for different pages. like if you want to limit characters on the shop page and want to keep full all other archive pages and home pages, you can do it by plugin Options.
Conclusion
with these two methods, I think you can solve your title limit character issues. if you still face issues please comment below, and we can discuss the issues in the comment below. please follow our Facebook page WordPress Tips and Trick or Subscribe to our Youtube Channel WordPress Beginner Tips