I'm pretty new to WP, so please forgive any stupid questions and/or coding!

I'm using the following code in my index.php file to include content from another page:

<div id="tm-container">
             $page = get_page_by_title('Today\'s Menu');
             $content = apply_filters('the_content', $page->post_content);
             echo $content.'<a href="'.get_permalink($post->ID).'">more>...</a>';;

It's working fine, but I'd like to truncate the content to say, 100 chars and include 'read more' at the bottom. I've tried using excerpt rather than content, but I need the html tags/formatting to be included.

I also can't use the quicktag <--!more--> on the 'Today's Menu' page as the site is going to be updated by various people who may not regularly use the internet and so are unlikely to be familiar with WP and its workings.

Specific assistance would be much appreciated as I'm quite new to WP!


3 Answers 3


Truncate the_content to your desired length, check if the last character is a < and remove it if so, then pass it to force_balance_tags, which will close any opened html tags.

  • "check if the last character is a <" . That doesn't seem right: what if the last character is, for example: "and <span>blabla </span" ?
    – pixeline
    Sep 8, 2011 at 7:40
  • @pixeline - force_balance_tags will recognize </span as an incomplete tag and fix it. </spa will not be recognized as an incomplete tag, however it will be ignored by the browser and not rendered, and force_balance_tags will add a proper closing tag, therefore the only case you have to worry about is <
    – Milo
    Sep 8, 2011 at 7:59
  • Thanks for this; I like the sound of force_balance_tags, I'm sure I'll be using that at some stage soon. As per my accepted answer, I've found a work-around that fits. However, your answer is really helpful so I've upvoted.
    – circey
    Sep 10, 2011 at 10:56

Well, since you've got a $content variable, why not try the PHP function substr()? http://php.net/manual/en/function.substr.php

  • Well to a point it works fine, but then it seems also to include tags which are (oddly) echoed like this: Penne w/bacon & cream, Lamb shanks w/mash, <. The < is the beginning of a new <p> tag.
    – circey
    Sep 8, 2011 at 3:08
  • Well, PHP isn't a person, so it doesn't know the difference between words and HTML tags :) In that case, I don't really have an answer for you. I imagine you could do something with regex, but I am definitely not the right person to ask. Sep 8, 2011 at 4:06
  • Yeah, I just assumed that as it wasn't echoing the tags that it was odd to echo '<', but without the rest of the tag I guess it's not a tag! Thanks for having a go!
    – circey
    Sep 8, 2011 at 4:15
  • 1
    try $content = substr(strip_tags($content),0,200); it's a php function that removes html tags.
    – pixeline
    Sep 8, 2011 at 7:38
  • Thaks Pixeline, strip_tags is helpful!
    – circey
    Sep 10, 2011 at 10:57

Okay, I worked it out - fudging it slightly, but it works. I kept everything as is, and added overflow:hidden on #tm-container, then moved the page link outside #tm-container.

The biggest problem was that the contributing page contained a menu with many paragraph breaks, which meant that I was going to be unable to control exactly where the content would stop (hence the < issue).

Using this solution (overflow & link outside container), means that it stops displaying content nicely at a certain height.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.