I have a theme for my mobile site that requires the <!--more-->
tag in my posts for it to display an excerpt of my post’s content.
I’ve got over 2000 posts already, and its going to take forever to add the tag in all my posts. I don’t want to use a plugin, cause I’ve used one that wasn’t stable at all.
I just need a snippet I can hook up in my theme’s files that will add the more tag after a specific character or word count in all my posts, and in every post I publish after that.
If you want to control the string count, then you can do it by the following.
Put the below code in the active theme’s functions.php file:–
For more details check the
Codex
The Read More (
<!--more-->
) tag is processed byget_the_content()
inwp-includes/post-template.php
. It does not run any hookable filters on its content input; however, that comes from the$pages
global which can be modified at the time it is set up by hooking thethe_post
action.The following function will auto-insert a
<!--more-->
tag at the first space after 280 characters (skipping HTML tags, and counting sequences of whitespace as a single character), if there isnât one already present:It should be fairly self-explanatory, though Iâll add a few notes on the regex:
u
(UTF-8) modifier ensures that multibyte characters count onlyas one.
+
(possessive) quantifier massively optimizes theperformance and memory footprint (where the token canât consume any
of the following token). However, it doesnât seem to make much difference
mitigating against failure when applied to the general repetition quantifier,
so this has been split into repetitions of repetitions. And nonetheless, in
the event of failure the content will be left unchanged.
?
(lazy) quantifier ensures the firstspace after 280 characters is stopped at, not the last.
(I know this question was asked more than 4 years ago, but it hadnât been answered; I found it looking for a solution for exactly the same, which I did not find elsewhere.)