2011-12-07 53 views
0

的代碼看起來像這樣如何在wordpress中全文顯示時刪除更多內容?

function new_excerpt_length($length) { 
    return 100; 
} 
add_filter('excerpt_length', 'new_excerpt_length'); 

存在可溼性粉劑管理員>設置>閱讀>對於飼料每篇文章的選項,顯示 如果設置爲全文 摘錄()必須返回全長文章而不是指定的長度。

該怎麼辦?

回答

1

好問題!答案很簡單:編寫你自己的功能!

打開您喜歡的編輯器了functions.php搗碎鍵盤上的隨機按鈕,直到你得到的東西是這樣的:

function my_awesome_excerpt ($post_id = false, $full = false, $length = 22) { 
    if (!$post_id) global $post; 
    else $post = get_post($post_id); 

    $text = $post->post_content; 

    if ($full) return $text; 

    else { 
     $text_array = explode(' ', $text); 
     $return_string = array(); 
     for ($i = 0; $i <= $length; $i++) 
      array_push($return_string, $text_array[$i]); 

     $new_awesome_string = '<p>'; 
     $new_awesome_string .= implode(' ', $return_string); 
     $new_awesome_string .= '</p><p class="readmore">'; 
     $new_awesome_string .= '<a href="' . get_permalink($post_id) . '">'; 
     $new_awesome_string .= 'Read More' . '</a></p>'; 

     return $new_awesome_string; 
    } 
} 

現在,你已經準備好最酷的部分。一旦你在你的循環,你可以寫出這樣的一些魔術:

echo my_awesome_excerpt(); 

它會自動地吐出摘錄。它使用全球后變量和一切!你甚至可以用它的循環之外:

echo my_awesome_excerpt($cpt->ID, 22); 

並設置自己的特殊長度!

或者你也許只是在你的心中知道這不值得,你只是想展示整件事情。那個樣子怎麼樣?

在循環中,你將不得不給它一個職位ID,對此感到抱歉。

echo my_awesome_script($post->ID, false); 

我希望這有助於。祝你有美好的一天!

+0

嗯不錯 但我已經通過檢查get_option()函數來完成。 :) – Bilal

相關問題