Splitting WordPress content with ‘more’ on single pages

WordPress uses the “More” button to create a small snippet of content, usually used on the blog page, to create article teasers and such. WordPress ignores the “More” function on single posts, pages and several other pages.

Here’s some code of make use of the “More” function on single pages and single posts. Perhaps splitting the content into columns or hiding extra content in an accordion or pretty much anything. Look, I’m going to press the “More” button right now.

So, obviously, this is the content after “More”. Great for a blog, but to make use of this on other pages, you need to create a new function.

Add the following to your theme’s functions file

function kb_split_content() {
	$content = preg_split('/<span id="more-\d+"><\/span>/i', get_the_content('more'));
	for($con = 0, $csize = count($content); $con < $csize; $con++) {
		$content[$con] = apply_filters('the_content', $content[$con]);
	}
	return $content;
}

This code returns an array of content split with “More”. You can split the content as many times as you like with more. The more splits, the more items in the array.

Once we’ve done that, you need to replace your theme’s the_content() on the relevant pages. In my recent example I was using a single-<custom-post-type>.php page.
So find the_content() and replace with;

&lt;?php
// show first half of the content
$content = kb_split_content();
echo $content[0];
?&gt;

Now, find where you want to add the second half of you content and use

&lt;?php 
if (isset($content[1]) AND !empty($content[1])) { // shows content after the more link 
     echo $content[1];
} 
?&gt;<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">


You can rinse and repeat if you have multiple splits;

&lt;?php 
if (isset($content[2]) AND !empty($content[2])) { // shows content after the more link 
     echo $content[2];
} 
?&gt;<span class="Apple-style-span" style="font-family: Georgia, 'Times New Roman', 'Bitstream Charter', Times, serif; font-size: 13px; line-height: 19px; white-space: normal;">

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *