Created
October 1, 2018 12:08
-
-
Save lamberger/365923f6451f827e6cf53fc311879b0a to your computer and use it in GitHub Desktop.
Multiple Custom Excerpt by WPExplorer Staff
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
| function wpex_get_excerpt( $args = array() ) { | |
| // Defaults | |
| $defaults = array( | |
| 'post' => '', | |
| 'length' => 40, | |
| 'readmore' => false, | |
| 'readmore_text' => esc_html__( 'read more', 'text-domain' ), | |
| 'readmore_after' => '', | |
| 'custom_excerpts' => true, | |
| 'disable_more' => false, | |
| ); | |
| // Apply filters | |
| $defaults = apply_filters( 'wpex_get_excerpt_defaults', $defaults ); | |
| // Parse args | |
| $args = wp_parse_args( $args, $defaults ); | |
| // Apply filters to args | |
| $args = apply_filters( 'wpex_get_excerpt_args', $defaults ); | |
| // Extract | |
| extract( $args ); | |
| // Get global post data | |
| if ( ! $post ) { | |
| global $post; | |
| } | |
| // Get post ID | |
| $post_id = $post->ID; | |
| // Check for custom excerpt | |
| if ( $custom_excerpts && has_excerpt( $post_id ) ) { | |
| $output = $post->post_excerpt; | |
| } | |
| // No custom excerpt...so lets generate one | |
| else { | |
| // Readmore link | |
| $readmore_link = '<a href="' . get_permalink( $post_id ) . '" class="readmore">' . $readmore_text . $readmore_after . '</a>'; | |
| // Check for more tag and return content if it exists | |
| if ( ! $disable_more && strpos( $post->post_content, '<!--more-->' ) ) { | |
| $output = apply_filters( 'the_content', get_the_content( $readmore_text . $readmore_after ) ); | |
| } | |
| // No more tag defined so generate excerpt using wp_trim_words | |
| else { | |
| // Generate excerpt | |
| $output = wp_trim_words( strip_shortcodes( $post->post_content ), $length ); | |
| // Add readmore to excerpt if enabled | |
| if ( $readmore ) { | |
| $output .= apply_filters( 'wpex_readmore_link', $readmore_link ); | |
| } | |
| } | |
| } | |
| // Apply filters and echo | |
| return apply_filters( 'wpex_get_excerpt', $output ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment