WordPress has inbuilt function force_balance_tags
which I belive is not explored much but it can be useful in such scenario.
get the content and truncate it to any arbitrary length
$content = get_the_content();
$length = 1000;
if(strlen($content) > $length) {
$content = substr($content, 0, $length);
}
Now apply force_balance_tags function to balance any html tags which were started in content but did not end due to string length limit.
echo force_balance_tags($content);
Hope it helps.
EDIT:
From the WordPress Documentation page
This function is used in the short post excerpt list, to prevent unmatched elements.
For example, it makes <div><b>This is an excerpt. <!--more--> and this is more text... </b></div> not break,
when the html after the more tag is cut off.
Related Posts:
- how to include page content to specified length & then “read more” link
- Get specific Blocks from Post
- Only show content before more tag
- post_content with line breaks
- Get content from one page and show it on another page
- Why is the_content() not displaying the content? [closed]
- Apply the_content filter for specific post types only
- How to to stop html editor from addig tags to shortcodes, images, etc
- Static page homepage not showing the_content
- How to add an inline style to the tag outputted in the_content() using PHP?
- Word limit in post_content after more tag
- Is it possible to handle a specific section of a post separately?
- How to encode post content as JSON?
- How to output only the post content
- How I can split text in the_content() into 2 columns?
- the_content() isn’t showing content, but $post->post_content does
- preg_replace Remove comment text in content
- tag the_content()
- Add span to the first letter of post
- Proper use of the_content filtering
- What is the proper way to get contents of a page?
- the_content is always surrounded by paragraphs (how to disable or remove them)?
- $content = $post->post_content; with formating
- Filter the_content() in the Quote Post Format to Display Quotes
- Cannot get grandparent object
- Get the Page Content,without generating HTML
- How to count rows of table in the_content()
- WordPress get_the_content get child element like p value
- Strip links from the_content
- Concatenate a Function to the Post Content
- How to change the position of Jetpack sharing icons? [closed]
- Loop after page content
- embed the_content
- Missing content in Pages only
- Short Overview in AMP Version and Link to full article non-amp [closed]
- Changing Image Size In The Content tag
- the_content() – Getting formatted text
- How to place HTML tags in content from the_content()?
- Proper way to replace the_content only for pages created by custom plugin
- limit how many words show up in the_content on index
- How can I display “read more” without any other post text?
- WordPress appends the_content filtered text inside last paragraph tag of content (rather than after it)
- how to display sidebar on pages in wordpress
- the_content(); of custom post type treated as an array or object
- How to keep before/after text inline with nl2br and the_content()
- removing tags around img, iframes and also scripts
- ECHO Executing 4 Times In Head
- Should I find/replace values in the_content?
- Remove images from the_content [duplicate]
- the_content of template page
- Extract a link in the_content()
- Content DISAPPEARS when simply adding normal text for ONE POST ONLY
- How do I show current post content in the header?
- Use content from one page in another template
- the_content() Not Grabbing All Content
- WordPress Load Only Page Text
- Display specific page (that is child) content on parent page
- Insert div after h2 in content
- How can I output the content of the page using this code?
- filter h1 tag to add a word at the beginning of the text
- the_content() brings the medium size image [closed]
- How can i specify the size of the image rendered by the_content();?
- IF Inside A Function For Content Filter [closed]
- content and excerpt not displaying
- Get Content Limit?
- Post Format Link Validation Error
- the_content returns blank when adding SEO details
- Adding if statement into the_content()
- How to add multiple the_content or something like that?
- Using str_replace on the_content and the_excerpt, but not working on archive index pages
- get_post() with filters applied
- Is there a difference in usage of the_title() and the_content()
- How to show Social share buttons outside the post?
- WordPress v5.4.2 escapes the content of Edit Attachment > Description field
- Remove pre and code tags from WordPress
- How to parse any content which was added after the working out filter ‘the_content’
- One Content Filter To Rule Them All
- Extract links inside embed tags in WordPress
- after setup_postdata, the_content() only displays text before the read more tag
- Remove P tag from images if aligncenter class is set
- Call the content help
- Remove a div with ID from the_content WordPress
- Modify Ixion Theme to Include Post Excerpt On Main Page
- get_post_fields as an excerpt
- The_content is different from category archive to other pages [closed]
- the_content inside my own theme does not retain line breaks / enters like in the admin
- the_content() not working in my Android mobile
- Making a specific page wordpress compatable that is not index
- the_content() Returns post content when I want page content
- how to print the content of pages inside the front end area
- Read more link for blog listing page
- Retrieve page content and pass to PHP?
- pre_get_posts returning unformatted page
- How do I remove the title from the_content
- Does WP have a hook for the use of wp_trim_words?
- I want a part of the_content be under custom post types
- Find and replace weird characters in the_content [closed]
- How to remove only images from the_content() [duplicate]
- Adding schema to text content in the loop, how?
- How can I customize the_content(); output? [closed]