Last active
July 31, 2019 15:11
-
-
Save Trovin/a717d3e36fc378eb5ab06a2c7e2a5c7a to your computer and use it in GitHub Desktop.
WP cropping post content 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
IN FUNCTION.PHP | |
=============== | |
<?php | |
/** | |
* Add post filter characters. | |
*/ | |
function content_excerpt( $args = '' ){ | |
global $post; | |
$default = array( | |
'maxchar' => 291, // количество символов. | |
'text' => '', // какой текст обрезать (по умолчанию post_excerpt, если нет post_content. | |
// Если есть тег <!--more-->, то maxchar игнорируется и берется все до <!--more--> вместе с HTML | |
'autop' => false, // Заменить переносы строк на <p> и <br> или нет | |
'save_tags' => '', // Теги, которые нужно оставить в тексте, например '<strong><b><a>' | |
'more_text' => 'Read more...', // текст ссылки читать дальше | |
); | |
if( is_array($args) ) $_args = $args; | |
else parse_str( $args, $_args ); | |
$rg = (object) array_merge( $default, $_args ); | |
if( ! $rg->text ) $rg->text = $post->post_excerpt ?: $post->post_content; | |
$rg = apply_filters('kama_excerpt_args', $rg ); | |
$text = $rg->text; | |
$text = preg_replace ('~\[/?.*?\](?!\()~', '', $text ); // убираем шоткоды, например:[singlepic id=3], markdown + | |
$text = trim( $text ); | |
// <!--more--> | |
if( strpos( $text, '<!--more-->') ){ | |
preg_match('/(.*)<!--more-->/s', $text, $mm ); | |
$text = trim($mm[1]); | |
$text_append = ' <a href="'. get_permalink( $post->ID ) .'#more-'. $post->ID .'">'. $rg->more_text .'</a>'; | |
} | |
// text, excerpt, content | |
else { | |
$text = trim( strip_tags($text, $rg->save_tags) ); | |
// Обрезаем | |
if( mb_strlen($text) > $rg->maxchar ){ | |
$text = mb_substr( $text, 0, $rg->maxchar ); | |
$text = preg_replace('~(.*)\s[^\s]*$~s', '\\1 ...', $text ); // убираем последнее слово, оно 99% неполное | |
} | |
} | |
// Сохраняем переносы строк. Упрощенный аналог wpautop() | |
if( $rg->autop ){ | |
$text = preg_replace( | |
array("~\r~", "~\n{2,}~", "~\n~", '~</p><br ?/>~'), | |
array('', '</p><p>', '<br />', '</p>'), | |
$text | |
); | |
} | |
$text = apply_filters('kama_excerpt', $text, $rg ); | |
if( isset($text_append) ) $text .= $text_append; | |
return ($rg->autop && $text) ? "<p>$text</p>" : $text; | |
} | |
IN TEMPLATE FILE | |
================= | |
<?php | |
$content = get_the_content(); | |
echo content_excerpt( array('maxchar'=>291, 'text'=>$content) ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment