Last active
November 2, 2017 07:51
-
-
Save itumulak/b21d844cfc437c954f207706994758ec to your computer and use it in GitHub Desktop.
Create an excerpt from the content if nothing is set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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