2013-03-07 2 views
1

Я пытался сделать ссылки, br и курсивом теги работает на ручном выдержке, но не повезло, нет отрывки. Я пробовал много Differents кодов, как предлагаемый в http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-preserve-html-tags-in-wordpress-excerpt-without-a-plugin/ и ничего, теперь у меня есть этот код:Wordpress html tags strip on manual excerpt

function wp_trim_all_excerpt($text) { 
global $post; 
$raw_excerpt = $text; 
//Add the allowed HTML tags separated by a comma 
$excerpt_length = apply_filters('excerpt_length', 150000); 
$text = wp_trim_words($text, $excerpt_length); //since wp3.3 
$allowed_tags = '<p>,<a>,<em>,<strong>,<i>,<br>'; 
$text = strip_tags($text, $allowed_tags); 

return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); //since wp3.3 
} 

remove_filter('get_the_excerpt', 'wp_trim_excerpt'); 
add_filter('get_the_excerpt', 'wp_trim_all_excerpt'); 

, но в $ allowed_tags ничего не спасти, я думаю, потому что у меня есть wp_trim, но я играл с ним в течение нескольких часов ничего, я также пробовал расширенный плагин excerpt, и я активировал выдержку на страницах, но, не знаю, почему, там работает html.

Любая идея?

ответ

0

Решение выше все еще имеет ограничение, поскольку некоторые HTML-теги встречаются парами. Если выдержка содержит открывающий тег, но не соответствующий закрывающий тег, то остальная часть веб-страницы скорее всего будет отформатирована неправильно.

Этот код проверяет выписку для таких тегов и добавляет закрывающие теги по мере необходимости:

<?php 
/****************************************************************************** 
* @Author: Richard Chonak 
* @Date: August 6, 2013 
* @Description: Ensures closure of HTML tags opened in an automatically generated excerpt. 
* Also trims off any trailing incomplete HTML tag at the end of the excerpt. 
* @Tested: Up to WordPress version 3.6 
* 
* @Author: Boutros AbiChedid 
* @Date: June 20, 2011 
* @Websites: http://bacsoftwareconsulting.com/ ; http://blueoliveonline.com/ 
* @Description: Preserves HTML formating to the automatically generated Excerpt. 
* Also Code modifies the default excerpt_length and excerpt_more filters. 
* @Tested: Up to WordPress version 3.1.3 
*******************************************************************************/ 
function custom_wp_trim_excerpt($text) { 
$raw_excerpt = $text; 
if ('' == $text) { 
    //Retrieve the post content. 
    $text = get_the_content(''); 

    //Delete all shortcode tags from the content. 
    $text = strip_shortcodes($text); 

    $text = apply_filters('the_content', $text); 
    $text = str_replace(']]>', ']]&gt;', $text); 

    $allowed_tags = '<img>,<small>,<strong>,<em>,<b>,<i>,<p>,<br>,<a>,<blockquote>,<ul>,<li>'; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/ 
    $twopart_tags = '<small>,<strong>,<em>,<b>,<i>,<p>,<br>,<a>,<blockquote>,<ul>,<li>'; /*** MODIFY THIS. Add the twopart HTML tags separated by a comma.***/ 
    /* turn tag list into one big search pattern */ 
    $search_patterns = "/" . str_replace(",","|",str_replace(">", "[^>]*>",$twopart_tags)) . '/'; 

    $text = strip_tags($text, $allowed_tags); 

    $excerpt_word_count = 200; /*** MODIFY THIS. change the excerpt word count to any integer you like.***/ 
    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 

    $excerpt_end = '[...]'; /*** MODIFY THIS. change the excerpt endind to something else.***/ 
    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 

    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY); 
    if (count($words) > $excerpt_length) { 
     array_pop($words); 
     $text = implode(' ', $words); 
     $text = $text . $excerpt_more; 
    } else { 
     $text = implode(' ', $words); 
    }; 


    /* if fragment ends in open tag, trim off */ 
    preg_replace ("/<[^>]*$/", "", $text); 

    /* search for tags in excerpt */ 
    preg_match_all ($search_patterns, $text, $matches); 
    /* if any tags found, check for matching pairs */ 
    $tagstack = array (""); 
    $tagsfound = $matches[0]; 
    while (count ($tagsfound) > 0) { 
     $tagmatch = array_shift($tagsfound); 
    /* if it's a closing tag, hooray; but if it's not, then look for the closer */ 
     if (!strpos($tagmatch,"</") && !strpos ($tagmatch,"/>")) { 
     preg_match("/\pL+/",$tagmatch, $tagwords); 
      $endtag = "</" . $tagwords[0] . ">"; 
      /* if this tag was not closed, put the closing tag on the stack */ 
      if (!in_array($endtag, $tagsfound)) { 
     array_push($tagstack,$endtag); 
      }; 
     }; 
    }; 

    /* if any unbalanced tags were found, add the closing tags */ 
    while (count ($tagstack) > 1) { 
    $text = $text . array_pop($tagstack); 
    } 
} 
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); 
} 
remove_filter('get_the_excerpt', 'wp_trim_excerpt'); 
add_filter('get_the_excerpt', 'custom_wp_trim_excerpt'); 
?> 

 Смежные вопросы

  • Нет связанных вопросов^_^