Skip to content

Instantly share code, notes, and snippets.

@leereamsnyder
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leereamsnyder/f2ecbc8a7049f428c80e to your computer and use it in GitHub Desktop.
Save leereamsnyder/f2ecbc8a7049f428c80e to your computer and use it in GitHub Desktop.
In WordPress loops, show the excerpt, the content before <!--MORE-->, or something else, but avoid showing the full post content.
<?php
/**
* Let's say you want to support BOTH post excerpts and use of the <!--more--> tag
* to truncate post content in The Loop.
*
* And let's also say that you definitely do not want to show the full content of the post
* if either of those are absent. Maybe you're displaying posts in little Pinterest-y
* cards so the text/excerpt can't be too long.
*
* Let's say this is your priority:
* 1. If there's an excerpt, use that. It's hand-crafted(tm)!
* 2. If there's a <!--more--> tag, use that. At least someone put thought into where to split things.
* 3. If neither of those are there, show nothing (or maybe just a link to the post). You don't want to rely on
* some sort of auto-truncation because truncation is not a content stra
*
* Some issues that come up:
* - <!--more--> tags don't work everywhere you might think. If you have a "static" home page (Settings -> Reading)
* with a Loop of posts, the_content() shows full content instead of just the pre-<!--more--> truncated content.
* - If you use the_content() as a fallback for excerpts, you might get the full text if there's no excerpt.
* if ( get_the_excerpt() ) { the_excerpt(); } else { the_content(); // could be the whole text }
* - If you're on a page where <!--more--> DOES work, the_content() and get_the_content() return
* the TRUNCATED content. So you can't do strstr( '<!--more-->', the_content() ) to detect it.
*
* So here's all the tricks I've got to prevent that.
*
* Use this within a Loop (obvs)
*/
// This makes sure <!--more--> works on pages where it might not, like a Static Front Page
// If you're going this way, make sure you don't use this code everywhere. It'll truncate posts if you do.
// http://codex.wordpress.org/Customizing_the_Read_More#How_to_use_Read_More_in_Pages
global $more;
$original_more = $more;
$more = 0;
if ( trim(get_the_excerpt()) ) :
// priority 1: if there's an excerpt, use that
the_excerpt();
elseif ( FALSE !== strstr( get_the_content('nonsensenonsense'), 'nonsensenonsense') ) :
/**
* here's where I get too clever for my own good:
* (get_)the_content() will be truncated now at <!--more-->. The actual "<!--more-->" string is gone.
* SO you can't just strstr(<!--more-->, get_the_content()) to detect IF there's a more tag.
* BUT, the first argument of (get_)the_content() is the text content for the More link.
* So I can put some nonsense text there, and strstr for that nonsense text.
* BOOM: you've detected if there's a valid <!--more--> tag there.
* If it's there, we can now show the (truncated) content
*/
the_content('',FALSE); // you can set that first parameter to whatever you want the more link text to be.
else:
?>
// whatever you want the fallback to be. let's say just a link to the post
<a href="<?php the_permalink(); ?>">Read the whole post&hellip;</a>
<?php
endif;
// After this and after you close The Loop, you may have to reset the global $more like follows:
// global $more;
// global $original_more;
// $more = $original_more;
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment