one review done, another article sort of half done desktop defragging, I'm off to bed! 8 hrs ago
  • Date
  • Thursday, October 2, 2008
  • Author
  • Corey Dutson

Getting more with the MORE tag

So who out there uses Word­press? I’ve been told it’s some­what pop­u­lar.

I myself am a fan despite it’s assault on the CPU and data­base. It’s fast, it’s simple, and so long as your web­site isn’t gain­ing huge traf­fic (or you’re paying peanuts for CPU usage and stor­age) than it’s a great selec­tion. It’s fairly cus­tomiz­able, has a huge sup­port and user base, and it’s just damned easy to use. I’ve been slowly get­ting more and more into cus­tomiz­ing and extend­ing what Word­press can do out of the box.There’s more in the code than people think.

Recently I a friend of mine ripped a strip off of me for only using sum­maries in my RSS feed. He told me that he, along with other net-​savvy users, didn’t have time to get teased by RSS summaries.

For those wish­ing to skip the lengthy buildup, here is a little table of con­tents:

The Back Story - To Summarize or not to Summarize

This is some what of a conun­drum, as I want people to actu­ally come to my site. On the flip side, I want people to read what I write. So I can force people to come to my site and gain page views, or I can increase my RSS read­er­ship. After care­ful con­sid­er­a­tion, I’ve opted to fix my RSS feed to dis­play the entire content.

Here’s the issue: Until now, I’ve used ‘The Excerpt’ field in Word­press. Basi­cally this allows me to have a pretty excerpt instead of 55 char­ac­ters trun­cated with a “READ MORE PLEAZE!” I use the custom excerpt for the top part of the post; the pre­view if you will. The prob­lem with this is that I had to rip out the first couple para­graphs from the post area, add some HTML to make sure it worked, and then post. The result looked pretty, but had some unex­pected side-​effects on the RSS.

As it turns out, when you decide to use the EXCERPT field, that excerpt will become the sum­mary in the RSS. That’s all well and good, but what If you want to have the whole post? One would hope that it would stitch the Excerpt and the Post together, but alas this was not the case.

I turned to the <!–more–> tag to assist me.

What they don’t doc­u­ment about the more tag is pretty much all the bitchy parts of it. When you use the <!–more–> tag, you split the con­tent, allow­ing for the excerpt to be defaulted to all the con­tent pre­ced­ing the tag. This is great, except for how my layout works. You see The top have is the Excerpt, and the bottom half is the rest of the post. Just using the_content wouldn’t work, because I would be repeat­ing all the pre-​more content.

I thought about maybe using an hr tag, or rework­ing my entire layout, but I dis­missed those due to the com­plex­ity of the markup. My only option was to carve into Word­press itself. I first checked Google to see if anyone had come up against what I was facing. As it turns out people have asked the ques­tion, but no one has posted the answer.

I am fixing that right now.

Getting the Pre and Post <!–more–> content separately

Here is my solu­tion, in full:

func­tion the_formatted_pre_more_from_content ($body)
{
$return­Val = get_the_formatted_pre_more_from_content ($body);
if ($return­Val !== FALSE)
echo get_the_formatted_pre_more_from_content ($body);
else
the_excerpt();
 }

func­tion get_the_formatted_pre_more_from_content ($body)
{
$more­Tag = ‘<!–more’;
$con­tent = FALSE;

$more­Pos = stripos($body, $more­Tag);
if ($more­Pos !== FALSE || $more­Pos > -1)
$con­tent = substr($body, 0, $more­Pos);
else
return FALSE;

$con­tent = apply_filters(’the_content’, $con­tent);
$con­tent = str_replace(’]]>’, ‘]]>’, $content);

return $con­tent;
 }

func­tion the_formatted_post_more_from_content ($body)
{
echo get_the_formatted_post_more_from_content ($body);
 }

func­tion get_the_formatted_post_more_from_content ($body)
{
$more­Tag = ‘<!–more’;
$con­tent = FALSE;

$more­Pos = stripos($body, $more­Tag);

if ($more­Pos !== FALSE || $more­Pos > -1)
{
$con­tent = substr($body, $more­Pos + strlen($moreTag));
$more­Pos = stripos($content, ‘–>’); // reuse vari­able
if ($more­Pos !== FALSE || $more­Pos > -1)
$con­tent = substr($content, $more­Pos + 3); // strip off rest of more tag
}
else
$con­tent = $body;

$con­tent = apply_filters(’the_content’, $con­tent);
$con­tent = str_replace(’]]>’, ‘]]&gt;’, $content);

return $con­tent;
 }

Explanation

The two impor­tant func­tions here are ‘get_the_formatted_pre_more_from_content’ and ‘get_the_formatted_post_more_from_content’. Long names, I know, but at least their mis­sion is clear.

The other two func­tions ‘the_formatted_pre_more_from_content’ and ‘the_formatted_post_more_from_content’ pretty much add a bit of logic and echo the con­tent auto­mat­i­cally. I chose this naming struc­ture and func­tion struc­ture to emu­late what is already in Word­press (e.g. ‘the_content’ versus ‘get_the_content’).

Installation and Usage

To use this code, add it all to your functions.php file of your theme. I’m sure I, or some other enter­pris­ing person, could turn this into a plugin, but at the moment I don’t feel it’s warranted.

All of the func­tions take one para­me­ter: $body.

When you call these func­tions you must be in The Loop.

exam­ple usage: <?php the_formatted_pre_more_from_content($post->post_content); ?>

expla­na­tion: This call will dis­play the con­tent pre­ced­ing the <!–more–> tag.

exam­ple usage: <?php the_formatted_post_more_from_content($post->post_content); ?>

expla­na­tion: This call will dis­play the con­tent pro­ceed­ing the <!–more–> tag.

Further Notes

Note the $post->post_content that is passed into the func­tion. This exists auto­mat­i­cally when you are in The Loop. This will pass all of the posts con­tent to the func­tion with­out any for­mat­ting. The only thing that isn’t straight text - con­ve­niently - is the <!–more–> tag. As a result the con­tent becomes fairly straight forward.

As of right now you must pass $post->post_content to the func­tions. I tried to do it with­out pass­ing the value, and they don’t seem to pick up the value.

I opted for using a sub­string func­tion­al­ity as opposed to an array split func­tion simply because it was 2 am when I finally got this going. I don’t know which is more effi­cient, so some­one who is more knowl­edge­able in PHP can com­ment on this and state which is better.

I only search for ‘<!–more’ because accord­ing to the tag doc­u­men­ta­tion, there is text that can follow the MORE that changes some func­tion­al­ity. As a result, I have an addi­tional if state­ment in the get_post func­tion that will detect for the end of the tag and sub­string the con­tent again to trim that out.

I hope that this helps out some people who may be in a sim­i­lar boat as I was.

Design Float Mixx Digg reddit del.icio.us StumbleUpon

Keep it clean, no spam, and thanks a bunch for any feedback you give.

*

*

*

No comments have been made yet.