Теперь Кью работает в режиме чтения

Мы сохранили весь контент, но добавить что-то новое уже нельзя

Как модифицировать the_excerpt()?

Как модифицировать the_excerpt() в Wordpress, чтобы бы ссылки отдавал, такими какие есть, а не убирал тег <a></a>?

HtmlWordpress+2
Oleg D.
  ·   · 745
Мы предоставим вам лучшие ответы и подробные...  · 9 нояб 2019  · mrwp.ru
Отвечает
Sergey Sergeev

Функция the_excerpt() - не принимает никаких параметров, который сокращает содержимое до 55 слов, и все теги HTML удаляются. Чтобы разрешить определенные или все теги HTML в выдержке, необходимо создать новую выдержку get_the_excerpt().

Вот функция которая позволит использовать все теги. После заданного количества слов и после последнего слова будет добавлен текст - «читать далее» .

function wpse_allowedtags() {
return '<script>,<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>'; // теги
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*Если вам нужно разрешить только определенные теги. Удалить, если все теги разрешены */
//Установите количество слов отрывка
$excerpt_word_count = 55;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Разделить строку на маркеры; HTML-теги или слова, за которыми следуют любые пробелы
preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_length && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
$excerptOutput .= trim($token);
break;
}
$count++;
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . ' » ' . sprintf(__( 'Читать далее: %s  »', 'wpse' ), get_the_title()) . '</a>';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0);
//else
// After the content
$wpse_excerpt .= $excerpt_more;
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');