Last active
August 28, 2021 07:55
-
-
Save andxbes/99bc5ad86a57f1fc73203b3b850a1f79 to your computer and use it in GitHub Desktop.
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
| <?php | |
| //... | |
| if( mb_strlen( $text ) > $rg['maxchar'] ){ | |
| $text = mb_substr( $text, 0, $rg['maxchar'] ); | |
| //$text = preg_replace( '/(.*)\s[^\s]*$/s', '\\1...', $text ); | |
| // прибиваем последнее слово | |
| $text = preg_replace( "%(.*)\s[^\s]*$%s", '\\1'.$rg['more_text'], $text ); // del last word, it not complate in 99% | |
| // если концом строки является обрубанный тег, рубаем его тоже. | |
| $text = preg_replace("%(.*)[<][^>]*{$rg['more_text']}$%s", '\\1'. $rg['more_text'], $text ); | |
| } | |
| //... |
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
| <?php | |
| function kama_excerpt($text = '', $args = []){ | |
| global $post; | |
| $defaults = [ | |
| 'maxchar' => 450, | |
| 'text' => '', | |
| 'autop' => false, | |
| 'more_text' => ' ...', | |
| 'ignore_more' => false, | |
| 'save_tags' => '<ul><ol><li><p><strong>', | |
| 'sanitize_callback' => 'strip_tags', | |
| ]; | |
| $rg = wp_parse_args($args, $defaults); | |
| $rg = apply_filters( 'kama_excerpt_args', $rg ); | |
| if( empty($text) ){ | |
| $text = $post->post_excerpt ?: $post->post_content; | |
| } | |
| // strip content shortcodes: [foo]some data[/foo]. Consider markdown | |
| $text = preg_replace( '~\[([a-z0-9_-]+)[^\]]*\](?!\().*?\[/\1\]~is', '', $text ); | |
| // strip others shortcodes: [singlepic id=3]. Consider markdown | |
| $text = preg_replace( '~\[/?[^\]]*\](?!\()~', '', $text ); | |
| // strip direct URLs | |
| $text = preg_replace( '~(?<=\s)https?://.+\s~', '', $text ); | |
| $text = trim( $text ); | |
| // <!--more--> | |
| if( ! $rg['ignore_more'] && strpos( $text, '<!--more-->' ) ){ | |
| preg_match( '/(.*)<!--more-->/s', $text, $mm ); | |
| $text = trim( $mm[1] ); | |
| $text_append = ' <a href="' . get_permalink( $post ) . '#more-' . $post->ID . '">' . $rg['more_text'] . '</a>'; | |
| } | |
| // text, excerpt, content | |
| else { | |
| $text = 'strip_tags' === $rg['sanitize_callback'] | |
| ? strip_tags( $text, $rg['save_tags']) | |
| : call_user_func( $rg['sanitize_callback'], $text, $rg ); | |
| $text = trim( $text ); | |
| // cut | |
| if( mb_strlen( $text ) > $rg['maxchar'] ){ | |
| $text = mb_substr( $text, 0, $rg['maxchar'] ); | |
| // $text = preg_replace( '/(.*)\s[^\s]*$/s', '\\1...', $text ); | |
| // прибиваем последнее слово | |
| $text = preg_replace( "%(.*)\s[^\s]*$%s", '\\1'.$rg['more_text'], $text ); // del last word, it not complate in 99% | |
| // если концом строки является обрубанный тег, рубаем его тоже. | |
| $text = preg_replace("%(.*)[<][^>]*{$rg['more_text']}$%s", '\\1'. $rg['more_text'], $text ); | |
| } | |
| } | |
| // add <p> tags. Simple analog of wpautop() | |
| if( $rg['autop'] ){ | |
| $text = preg_replace( | |
| [ "/\r/", "/\n{2,}/", "/\n/", '~</p><br ?/?>~' ], | |
| [ '', '</p><p>', '<br />', '</p>' ], | |
| $text | |
| ); | |
| } | |
| $text = apply_filters( 'argo_kama_excerpt', $text, $rg ); | |
| if( isset( $text_append ) ){ | |
| $text .= $text_append; | |
| } | |
| return ( $rg['autop'] && $text ) ? "<p>$text</p>" : $text; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
было так что <p class="">Text заменяло на <p ...