Use PHP string functions like substr to return a number of characters rather than word count.
The post content aaaaaaaaaaaaaaa aaaaaaaaaaaaa will be much longer than a a.
$max_length = 150;
echo substr( $string, 0, $max_length );
As a function it might look like:
function string_limit_words( $string = '', $count = 25, $after="..." ) {
if ( strlen( $string ) <= $count ) {
return $string;
}
return substr( $string, 0, $count ) . $after;
}
echo string_limit_words( "This is a lot of words", 10 );
// This is a ...
the_excerpt() prints the output while get_the_excerpt() will return the string to allow manipulation before output.
Related Posts:
- How to Add Image to WordPress RSS-Feed with no Plug-in?
- Disable comment rss feeds for pages in wordpress
- Getting Post Thumbnail from RSS feed with SimplePie
- Limiting the WordPress feed to only a certain limit of characters
- Editing Category RSS Feeds
- Remove a div from RSS feed
- get post content of particular post by url
- Display image from an external rss feed on wordpress without plugins
- Can’t retrieve element with WordPress default @fetch_rss();
- Include only posts with meta key “add_in_rss” in RSS Feed
- Can I add the post featured image to a specific RSS feed?
- change default RSS feed URL
- Display WordPress Post on A Different Site
- Aspx/Rss feed – failed to open stream: Redirection limit reached
- Good RSS Feed Parser to use on php file?
- Disable category RSS Feeds on WordPress blog with PHP
- How to access all meta data for RSS feed?
- WordPress create custom XML RSS feed template
- A custom RSS feed link, how do I modify the XML structure?
- How do I limit the number of forms a user can create in gravity forms?
- Creating sub-categories via php
- How can i display a jquery slide on front page
- Add post with approval by the admin
- Widget back end radio button issue
- Count posts on multisite with blog id
- how to make jpg downdable in wordpress?
- Troll the hackers by redirecting them
- Putting php inside an echo php shortcode
- GET problem in WordPress
- Almost Done… Post Format Code
- InnerBlocks with allowedBlocks not working with acf_register_block
- How to unserialize data from MySQL
- if ( ! function_exists
- Exclude product attributes from taxonomy terms loop
- WP All Import / Update stock quantity from multiple XML files
- PHP -> SQL Query with Summing
- Split site title and apply different classes
- Alter Woocommerce product archive structure
- Pass get data from anchor URL [closed]
- WordPress İmages Upload & Delete Error
- How to style injected code in header section?
- Shortcode for a Series of Elevating Image Files, Excluding a Range?
- Shortcode won’t execute
- Dynamic CSS through PHP issue
- posts_nav_link pulling in multiple post types
- Echo array value
- How to break up php code to avoid echo
- Remove password protection from all posts
- security concerns if using html data-* attribute for l10n?
- Ajax on the Administration Side of plugin – wp_localize_script – how to pass value from JQuery to PHP function in class?
- php_gd2 extension not loading on Windows
- Search bar for wp menu
- How to check a variable Changes
- Products listing check if meta checkbox is checked
- Check if user had autologin & if so, logout
- White screen after migrating site, but wp-admin works fine. Disabling all plugins, applying default theme and enabling debug mode does nothing
- Executing php on button press
- random post redirect in wordpress with a .php file
- require get_template_directory() . ‘path/to-my/file.php’ BREAKS customize > themes functionality
- How do I change the “login” label in My Account page to “Registered Customer”
- Edit a function to take different actions based on user role
- How to have different headers.php files based on the display page
- Customize the “Available for back order” text for specific variation
- If user has clicked link add class and store using PHP/WordPress
- How to put 2 php codes in functions.php without site crashing
- How to add custom attribute to all outofstock and all instock products automatically?
- Site infected by link
- sessionStorage saves input value in browser but it is not loaded in the form field
- Multiple Arrays for Custom Post Query using Boostrap accordions and ACF repeater fields
- How to create an Email Subscription input in WordPress?
- autocomplete in wordpress using ajax with json-data
- Using previous_post_link next_post_link but with a filter on a custom field
- How to filter specific element of an array in wordpress/php?
- wp-login.php?redirect_to=https problem
- Right path for renaming a file in uploads folder
- View tag description on page
- I’m writing my first plugin and my echo output is showing up on every page of my wordpress installation
- Beyond widget side menu editing, with the php page, custom template
- wp_delete_comment is doing nothing
- Using page template to fetch posts in page
- Adding a “Word” in php code
- conditionally load a default static image if image metabox has no value
- MySQL queries in WordPress
- How to add a code for lastpostdate after/inside a specific menu link?
- Let users register weight each day and save it in DB
- Adding code to the function file
- Query specific posts per user selections from dropdown menus
- Execute PHP code in Javascript onclick
- why my “recepie” category is not taken into account in my request
- How to show blog posts changing every time after refreshing the page in non-wordpress website?
- php 7.0 update to 7.1 and at least 7.2
- Exclude Everywhere but Admin Area?
- Use URL query string in img src attribute
- Getting a specific “title for a section” only on Startpage?
- In WordPress how do you remove render-blocking CSS manually without a plugin?
- how to check elementor is widget is active or loaded
- ask about add_rewrite_endpoint query_vars
- PHP – Having $_SESSION as an array and adding $_SESSION to array
- Cannot Access ACF Field Values via my Plugin
- Remove slug and custom post type name from URL