..here to make your wordpress site better

Understanding WordPress the_content Function

The php function the_content is one of the most widely used functions within a standard WordPress Blog. This article explains the details of this popular function, along with some hacks that can be used to manipulate the output the_content provides.

What does the_content do?

Well for starters, the_content must be used within the loop (more on this in a later post, but for now just understand that the loop is the function used to loop through posts and pull them from your database). This functions main purpose is to display the post content – it’s normally used alongside other functions such as the_title and the_tags to form meaningful copy.

How is the_content used?

In it’s most basic form, you’d insert <?php the_content();?> wherever you wish to display the full post content. As mentioned previously, this only works within the loop.

If you’d prefer an excerpt (a few lines perhaps?) to be displayed instead of the full content, you can use the <!–more–> tag when you’re writing your post. This will then tell the_content function to only output content up till this tag – unless it’s being used on a single page – in which case you probably want the full content displayed anyway! This is useful when using the_content on index pages, as you don’t always want the full post content shown.

What else can the_content do?

Well the_content function takes 2 parameters – it’s up to you whether you include these or not. The 2 parameters it accepts are: $more_link_text and $stripteaser. These are used as follows:

$more_link_text – This describes the text that will be inserted if the_content comes across a <!–more–> tag. For example – if you’d like ‘Read More’ to be shown, you’d use <?php the_content(‘Read More’);?>. This would then output the beginning part of the post, and then a clickable Read More link to take the reader to the full single page post.

$stripteaser – This parameter takes either a 1 or a 0. By default, it’s set to 0 (false). The purpose of this parameter is to strip the teaser (the content found before the more tag) if it’s set to 1 (true).

So if you wanted to display only the post content that appears after the <!–more–> tag, you’d use:

<?php the_content(”, 1);?>.

There aren’t many occasions where we’d need to chance this parameter – but it’s available if you can find a reason to use it.

What if I don’t want to use the <!–more–> tag to shorten the content?

There is a simple hack that can be used to define a set length for the_content, without having to insert a <!–more–> tag every time you only want to show an excerpt. This can be useful if you’ve already written hundreds of posts, or if your new theme isn’t displaying things how you’d like.

To do this, we’ll use the php function ‘substr’. This function takes 3 parameters as follows:

<?php substr($string, $start, $length);?>

$string – for this, we’d pass it get_the_content (which outputs the_content but in plain text without formatting).
$start – The first character count we want to display.
$length – The amount of character count we want to display.

So to show the first 400 characters of a post, we’d use:

<?php substr(strip_tags(get_the_content()), 0, 400);?>

Change the final number to adjust how many characters get pulled back for each post.

Posted in: Tutorials
Get the latest Wordpress News delivered direct to your mailbox

Comments (6)

 

  1. This is a good tutorial, but I’ve been racking my brain and I can’t think of a single good reason why you’d want to use $stripteaser; it just seems so redundant. I wonder why it ever got included…

    • Steve says:

      Hey Dave, I can think of one good reason for using it – how about showing all your posts on your index page with a really enticing excerpt, but then when viewing the single version of the post, using $stripteaser to remove the excerpt and show the content. I guess once a reader has made it to the single post, they don’t need to read the original excerpt that made them click?!

  2. Sagar says:

    the_excerpt function in wp shows readmore link.. if i want to show readmore link on conditional basis then how i do that..

    • Steve says:

      That’s an unusual question, not one I’ve really thought about before.

      Presuming you want to define whether a ‘Read More’ link is shown while writing the post – I’d probably write my own function using a ‘custom field’ defined for each post.

      Let me know if you need more help on this and I’ll knock something together.

  3. Vik says:

    Wow, really great and helpful post.
    was looking for a solution since so long.

    Thanks !!!

Leave a Reply

  • Name
  • Mail
  • Website
  • Comment