douzoudang1511 2015-08-28 16:00
浏览 69
已采纳

Wordpress摘录功能,输出不良

My first question here:

I am using this code to show excerpts on my Wordpress. It allows me to show tags within the excerpt and it works OK, but the problem is when a have a word that is linked, after it I always get one space before the comma.

Example:

<a href="www.google.com">Google</a>, <a href="www.yahoo.com">Yahoo</a>

Output:

Google , Yahoo

The space after the comma is on purpose and it should be there. The one before is one too much.

Any tips on how to fix this?

Code that I use in order to define excerpt:

function new_wp_trim_excerpt($text) {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        $text = get_the_content('');

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $text = strip_tags($text, '<a>');
        $excerpt_length = apply_filters('excerpt_length', 70);


        $words = preg_split('/(<a.*?a>)|
||\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
        if ( count($words) > $excerpt_length ) {
            array_pop($words);
            $text = implode(' ', $words);
            $text = $text . $excerpt_more;
        } else {
            $text = implode(' ', $words);
        }
    }
    return apply_filters('new_wp_trim_excerpt', $text, $raw_excerpt);

}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'new_wp_trim_excerpt');
  • 写回答

1条回答 默认 最新

  • duanmei9980 2015-08-28 17:14
    关注

    It appears to be an issue with your regex, if you add a comma after the <a.*?a> does that help? Here's the regex line with the comma included:

    $words = preg_split('/(<a.*?a>,)|
    ||\t|\s/', $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助