Skip to content

Instantly share code, notes, and snippets.

@itumulak
Last active November 2, 2017 07:51
Show Gist options
  • Select an option

  • Save itumulak/b21d844cfc437c954f207706994758ec to your computer and use it in GitHub Desktop.

Select an option

Save itumulak/b21d844cfc437c954f207706994758ec to your computer and use it in GitHub Desktop.
Create an excerpt from the content if nothing is set
/**
* Allows to switch between excerpt and content.
* If excerpt is not set we will use content but limit the words.
* Ideally use for custom queries.
*
* @param string $content
* @param string $excerpt
* @param int $word_limit
* @param string $more
* @return string $trimmed_content
*/
function content_switch($content, $excerpt, $word_limit = '', $more = ' ... ')
{
if ( empty($excerpt) )
{
$excerpt = wpautop( $content );
$excerpt = substr( $excerpt, 0, strpos( $excerpt, '</p>' ) + 4 );
$excerpt = strip_tags($excerpt, '<a><strong><em>');
if (is_int($word_limit) && $word_limit > 0)
$excerpt = wp_trim_words($excerpt, $word_limit, $more);
}
return apply_filters( __NAMESPACE__ . '\\trim_content', $excerpt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment