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.