修改默认情况下的excerpt参数

自定义WordPress文章摘取与首页展示个性化设置
本文提供了一种方法,教你如何在WordPress中调整文章摘取长度,并实现首页文章展示的个性化定制,包括使用不同插件和自定义代码实现全文或摘要展示。

在wp-includes目录下找到formatting.php,里面有这样一个函数:

  1. function   wp_trim_excerpt ( $text ) {
  2.     global   $post ;
  3.     if   ( " == $text ) {
  4.         $text = get_the_content ( " ) ;
  5.         $text = apply_filters ( ' the_content ' , $text ) ;
  6.         $text = str_replace ( ' ]]> ' , ' ]]> ' , $text ) ;
  7.         $text = strip_tags ( $text ) ;
  8.         $excerpt_length = 55 ;
  9.         $words = explode ( '   ' , $text , $excerpt_length + 1 ) ;
  10.         if   ( count ( $words ) > $excerpt_length ) {
  11.             array_pop ( $words ) ;
  12.             array_push ( $words , ' [...] ' ) ;
  13.             $text = implode ( '   ' , $words ) ;
  14.         }
  15.     }
  16.     return   $text ;
  17. }

$excerpt_length = 55 改为你需要截取的字数。


  • 对于中文文章的WordPress
  • 如果你使用了《中文 WordPress 工具箱》插件,则可以在/wp-content/plugins/mulberrykit.php中找到函数mul_excerpt ($excerpt):

    function mul_excerpt ($excerpt) {
         $myexcerpt = substr($excerpt,0,255);
         return utf8_trim($myexcerpt) . '...';
    }

    将其中的255修改为你想截取的字数。

    本文可以自由转载,转载时请保留全文并注明出处:
    转载自仲子说 [ http://www.wangzhongyuan.com/ ]
    原文链接:

     

    DIY个性Wordpress首页文章

    1,打开你的index.php或home.php,找到:

    1. < ?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    2,在这一句的前面加上代码:

    1. < ?php $postcnt = 1; ?>

    3,然后找到

    1. < ?php the_content(); ?>
    并把它修改为:
    1. < ?php if ( $postcnt == 1) : ?>
    2. < ?php the_content(); ?>
    3. // 这里还可以放一个广告,效果很好的位置.
    4. < ?php else : ?>
    5. < ?php the_excerpt(); ?>
    6. < ?php endif ; $postcnt ++; ?>

    上传以后需要到你的wordpress后台设置为输出样式,输出全文则第一篇为全文,输入摘要则显示< !–more–>标签之前内容。< ?php the_excerpt(); ?>是输出去掉html以后的文字的函数,不过对中文不是很友好,建议配合使用Yskin’s wp-CJK-excerpt 来解决摘要的问题,也可使用中文WordPress工具箱 或其他摘要算法的插件。

if (!function_exists('get_article_for_category')) { function get_article_for_category( $category, $posts_per_page = 5, $tag = '' ,// 新增参数:标签(tag) $include_meta = true, $include_acf = true, $orderby = 'menu_order', $order = 'DESC' ) { // 限制只允许的排序字段 $allowed_orderby = array('date', 'menu_order'); if (!in_array($orderby, $allowed_orderby)) { $orderby = 'date'; // 默认回退到按时间排序 } // 设置基础查询参数 $args = array( 'posts_per_page' => $posts_per_page, 'orderby' => $orderby, 'order' => $order, ); // 添加分类查询条件 if (is_numeric($category)) { $args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'term_id', 'terms' => $category, ); } else { $args['tax_query'][] = array( 'taxonomy' => 'category', 'field' => 'slug', 'terms' => $category, ); } if (!empty($tag)) { $args['tax_query']['relation'] = 'AND'; if (is_numeric($tag)) { $args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'term_id', 'terms' => $tag, ); } else { $args['tax_query'][] = array( 'taxonomy' => 'post_tag', 'field' => 'slug', 'terms' => $tag, ); } } if ($orderby === 'menu_order') { $args['orderby'] = 'menu_order'; } $query = new WP_Query($args); if ($query->have_posts()) { $posts = array(); while ($query->have_posts()) { $query->the_post(); $post_id = get_the_ID(); $post_data = array( 'id' => $post_id, 'title' => get_the_title(), 'permalink' => get_the_permalink(), 'excerpt' => get_the_excerpt(), 'content' => get_the_content(), 'thumbnail' => has_post_thumbnail() ? get_the_post_thumbnail_url() : '', 'date' => get_the_date(), 'author' => get_the_author(), 'modified_date' => get_the_modified_date(), 'type' => get_post_type(), 'status' => get_post_status(), 'comment_count' => get_comments_number(), ); // 获取所有自定义字段(post meta) if ($include_meta) { $meta = get_post_meta($post_id); $post_data['meta'] = $meta; } // 如果使用了 ACF,获取 ACF 字段 if ($include_acf && function_exists('get_fields')) { $acf_fields = get_fields(); if ($acf_fields) { $post_data['acf'] = $acf_fields; } } $posts[] = $post_data; } wp_reset_postdata(); return $posts; } return false; } }
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值