2011-04-03 76 views
0

我有這個功能。Wordpress自動修剪the_content問題

function trim_the_content($the_contents = '', $read_more_tag = '...READ MORE', $perma_link_to = '', $all_words = 100) { 
    // make the list of allowed tags 
    $allowed_tags = array('a', 'abbr', 'b', 'blockquote', 'b', 'cite', 'code', 'div', 'em', 'fon', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'hr', 'i', 'label', 'i', 'p', 'pre', 'span', 'strong', 'title', 'ul', 'ol', 'li', 'object', 'embed', 'param'); 
    if($the_contents != '' && $all_words > 0) { 
     // process allowed tags 
     $allowed_tags = '<' . implode('><', $allowed_tags) . '>'; 
     $the_contents = str_replace(' ]]>', ' ]]>', $the_contents); 
     $the_contents = strip_tags($the_contents, $allowed_tags); 
     // exclude HTML from counting words 
     if($all_words > count(preg_split('/[\s]+/', strip_tags($the_contents), -1))) return $the_contents; 
     // count all 
     $all_chunks = preg_split('/([\s]+)/', $the_contents, -1, PREG_SPLIT_DELIM_CAPTURE); 
     $the_contents = ''; 
     $count_words = 0; 
     $enclosed_by_tag = false; 
     foreach($all_chunks as $chunk) { 
      // is tag opened? 
      if(0 < preg_match('/<[^>]*$/s', $chunk)) $enclosed_by_tag = true; 
      elseif(0 < preg_match('/>[^<]*$/s', $chunk)) $enclosed_by_tag = false; 
      if(!$enclosed_by_tag && '' != trim($chunk) && substr($chunk, -1, 1) != '>') $count_words ++; 
      $the_contents .= $chunk; 
      if($count_words >= $all_words && !$enclosed_by_tag) break; 
     } 
       // note the class named 'more-link'. style it on your own 
     $the_contents = $the_contents . '' . $READ_MORE_TAG . ''; 
     // native WordPress check for unclosed tags 
     $the_contents = force_balance_tags($the_contents); 
    } 
    return $the_contents; 
} 

當我在我的索引中調用函數時,「Read More ...」丟失。

<?php 
// here's the point where we are about to display Post excerpt 
$perma_link = get_permalink($post->ID); // current Post permalink 
$content = get_the_content(); // save entire content in variable 
$content = apply_filters('the_content', $content); // WP bug fix 
$content = str_replace(']]>', ']]>', $content); // ...as well 
echo trim_the_content($content, __("READ MORE", "sofa_memento"), $perma_link, 100); 
?> 

有沒有人看到我的問題?

+1

$ READ_MORE_TAG處於大寫狀態?/$ read_more_tag ='...閱讀更多' – 2011-04-03 01:15:07

回答

3
$the_contents = $the_contents . '' . $read_more_tag . ''; 
+0

或者只是:'$ the_contents = $ the_contents。$ read_more_tag;'...不需要無聊的單引號。 – Christian 2011-04-03 01:19:56

+0

是的,顯然沒有額外的需要,只是想找到更快的行;/ – 2011-04-03 01:30:21

+1

我的評論主要是針對OP,對不起,如果它看起來不對。 – Christian 2011-04-03 01:31:33