网站地图    收藏   

主页 > 后端 > wordpress教程 >

WordPress the_excerpt()函数文章摘要字数并加上链接

来源:自学PHP网    时间:2014-11-28 23:41 作者: 阅读:次

[导读] the_excerpt()函数对于数字控制与给摘要带连接的技巧,希望此文章对各位会有所帮助 WordPress里显示文章摘要的函数the_excerpt()默认是显示55个字,对于一些模板来说,只显示55个字的摘要貌似有...

WordPress the_excerpt()函数文章摘要字数并加上链接

the_excerpt()函数对于数字控制与给摘要带连接的技巧,希望此文章对各位会有所帮助.

WordPress里显示文章摘要的函数the_excerpt()默认是显示55个字,对于一些模板来说,只显示55个字的摘要貌似有些短,因此我们有必要在模板函数functions.php里面对the_excerpt()做个改造,使之按着我们的需求来展示更多(或更少)的摘要,改动很简单,在functions.php里加上这么一段即可:

  1. function emtx_excerpt_length( $length ) { 
  2.  return 92; //把92改为你需要的字数,具体就看你的模板怎么显示了。 
  3. } 
  4. add_filter( 'excerpt_length', 'emtx_excerpt_length' ); 

上面的只是对数字控制了,那么要如何给字符带上链接地址呢.处理方法很简单,我们只要往主题的functions.php里加上这么一段代码:

  1. function emtx_continue_reading_link() { 
  2.  return ' <a href="'. get_permalink() . '">查看全文&rarr;</a>'; 
  3. } 
  4.  
  5. function emtx_auto_excerpt_more( $more ) { 
  6.  return ' &hellip;' . emtx_continue_reading_link(); 
  7. } 
  8. add_filter( 'excerpt_more', 'emtx_auto_excerpt_more' ); 
  9.  
  10. function emtx_custom_excerpt_more( $output ) { 
  11.  if ( has_excerpt() && ! is_attachment() ) { 
  12.   $output .= emtx_continue_reading_link(); 
  13.  } 
  14.  return $output; 
  15. } 
  16. add_filter( 'get_the_excerpt', 'emtx_custom_excerpt_more' ); 

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论