Я использую wp_trim_words, чтобы обрезать некоторые отрывки на моей домашней странице. Он работает нормально, за исключением того, что из отрывков удаляются теги HTML. Мне нужно иметь возможность выделять определенные фрагменты отрывка жирным шрифтом (с помощью ). Следуя инструкциям здесь, я попытался удалить функцию wp_trim_words и заменить ее новой, используя следующий код, который заменяет $text = wp_strip_all_tags( $text); из исходной функции WP на $text = Strip_tags($ текст, '',);. Но это ломает сайт. Что я делаю не так?
// Remove Reverie Trim Words
function remove_trim_words() {
remove_filter('get_the_excerpt', 'wp_trim_words');
add_filter('get_the_excerpt', 'oakwood_trim_words');
}
// Replace Reverie Trim Words
function oakwood_trim_words( $text, $num_words = 55, $more = null ) {
if ( null === $more )
$more = __( '…' );
$original_text = $text;
$text = strip_tags($text, '',);
/* translators: If your word count is based on single characters (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$sep = '';
} else {
$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$sep = ' ';
}
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
} else {
$text = implode( $sep, $words_array );
}
/**
* Filter the text content after words have been trimmed.
*
* @since 3.3.0
*
* @param string $text The trimmed text.
* @param int $num_words The number of words to trim the text to. Default 5.
* @param string $more An optional string to append to the end of the trimmed text, e.g. ….
* @param string $original_text The text before it was trimmed.
*/
return apply_filters( 'oakwood_trim_words', $text, $num_words, $more, $original_text );
}
Подробнее здесь: https://stackoverflow.com/questions/241 ... in-excerpt
Как запретить Wordpress удалять HTML-теги в отрывке ⇐ Php
Кемеровские программисты php общаются здесь
-
Anonymous
1731255110
Anonymous
Я использую wp_trim_words, чтобы обрезать некоторые отрывки на моей домашней странице. Он работает нормально, за исключением того, что из отрывков удаляются теги HTML. Мне нужно иметь возможность выделять определенные фрагменты отрывка жирным шрифтом (с помощью ). Следуя инструкциям здесь, я попытался удалить функцию wp_trim_words и заменить ее новой, используя следующий код, который заменяет $text = wp_strip_all_tags( $text); из исходной функции WP на $text = Strip_tags($ текст, '',);. Но это ломает сайт. Что я делаю не так?
// Remove Reverie Trim Words
function remove_trim_words() {
remove_filter('get_the_excerpt', 'wp_trim_words');
add_filter('get_the_excerpt', 'oakwood_trim_words');
}
// Replace Reverie Trim Words
function oakwood_trim_words( $text, $num_words = 55, $more = null ) {
if ( null === $more )
$more = __( '…' );
$original_text = $text;
$text = strip_tags($text, '',);
/* translators: If your word count is based on single characters (East Asian characters),
enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */
if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) {
$text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' );
preg_match_all( '/./u', $text, $words_array );
$words_array = array_slice( $words_array[0], 0, $num_words + 1 );
$sep = '';
} else {
$words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY );
$sep = ' ';
}
if ( count( $words_array ) > $num_words ) {
array_pop( $words_array );
$text = implode( $sep, $words_array );
$text = $text . $more;
} else {
$text = implode( $sep, $words_array );
}
/**
* Filter the text content after words have been trimmed.
*
* @since 3.3.0
*
* @param string $text The trimmed text.
* @param int $num_words The number of words to trim the text to. Default 5.
* @param string $more An optional string to append to the end of the trimmed text, e.g. ….
* @param string $original_text The text before it was trimmed.
*/
return apply_filters( 'oakwood_trim_words', $text, $num_words, $more, $original_text );
}
Подробнее здесь: [url]https://stackoverflow.com/questions/24151161/how-to-prevent-wordpress-from-stripping-html-tags-in-excerpt[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия