the_excerpt()函數對于數字控制與給摘要帶連接的技巧,希望此文章對各位會有所幫助.
WordPress里顯示文章摘要的函數the_excerpt()默認是顯示55個字,對于一些模板來說,只顯示55個字的摘要貌似有些短,因此我們有必要在模板函數functions.php里面對the_excerpt()做個改造,使之按著我們的需求來展示更多(或更少)的摘要,改動很簡單,在functions.php里加上這么一段即可:
- function emtx_excerpt_length( $length ) {
- return 92; //把92改為你需要的字數,具體就看你的模板怎么顯示了。
- }
- add_filter( 'excerpt_length', 'emtx_excerpt_length' );
上面的只是對數字控制了,那么要如何給字符帶上鏈接地址呢.處理方法很簡單,我們只要往主題的functions.php里加上這么一段代碼:
- function emtx_continue_reading_link() {
- return ' <a href="'. get_permalink() . '">查看全文→</a>';
- }
- function emtx_auto_excerpt_more( $more ) {
- return ' …' . emtx_continue_reading_link();
- }
- add_filter( 'excerpt_more', 'emtx_auto_excerpt_more' );
- function emtx_custom_excerpt_more( $output ) {
- if ( has_excerpt() && ! is_attachment() ) {
- $output .= emtx_continue_reading_link();
- }
- return $output;
- }
- add_filter( 'get_the_excerpt', 'emtx_custom_excerpt_more' );
新聞熱點
疑難解答
圖片精選