Skip to content

Instantly share code, notes, and snippets.

@dylanized
Created April 24, 2012 23:13
Show Gist options
  • Save dylanized/2484502 to your computer and use it in GitHub Desktop.
Save dylanized/2484502 to your computer and use it in GitHub Desktop.
Flexible WP Excerpt Snippet
<?php // outside the loop
// empty more text on custom excerpt
function custom_excerpt_more( $more ) {
return '';
}
add_filter( 'excerpt_more', 'custom_excerpt_more' );
// instantiate global excerpt string
$more_text = "Read More&hellip;";
?>
<?php // inside the loop
if (!is_singular()) {
global $more_text;
if (has_excerpt()) {
// if it has a custom excerpt
the_excerpt();
?>
<p><a class="more-link" href="<?php the_permalink() ?>"><?= $more_text ?></a></p>
<?php
} elseif (strpos($post->post_content, '<!--more-->')) {
// if it it has a break tag
the_content($more_text);
} else {
// display the default excerpt
the_excerpt();
?>
<p><a class="more-link" href="<?php the_permalink() ?>"><?= $more_text ?></a></p>
<?php
}
} else {
the_content();
edit_post_link('Edit Post', '<div><small>', '</small></div>');
} ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment