duanbarong4617 2014-03-27 02:59
浏览 19

截断WordPress发布内容而不会丢失HTML格式

I'm working on a WordPress theme where I need to truncate the post at a certain number of words. I understand how to use the_excerpt(), however this strips out all the paragraph breaks, links, etc. which is NOT the desired effect. I tried using jQuery Succinct and applying that to the_content() -- that maintained the formatting, but it cut off in the middle of a paragraph so I had an open <p> that then broke the rest of the layout. The client does not want to use the option to manually insert a "more" tag into the post.

Is there a way I can do this either via PHP or jQuery?

  • 写回答

1条回答 默认 最新

  • dongqun9403 2014-03-27 04:20
    关注

    You have to create your own excerpt function. I have written one that keeps all html tags in tact and also cut the excerpt at the end of a sentence just after the chosen amount of words.

    You need to remove the original excerpt filter first and add your new one. Add this to your functions.php

    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'pietergoosen_custom_wp_trim_excerpt'); 
    

    Now add this below

    function pietergoosen_custom_wp_trim_excerpt($pietergoosen_excerpt) {
        global $post;
        $raw_excerpt = $pietergoosen_excerpt;
            if ( '' == $pietergoosen_excerpt ) {
    
                $pietergoosen_excerpt = get_the_content('');
                $pietergoosen_excerpt = strip_shortcodes( $pietergoosen_excerpt );
                $pietergoosen_excerpt = apply_filters('the_content', $pietergoosen_excerpt);
                $pietergoosen_excerpt = str_replace(']]>', ']]&gt;', $pietergoosen_excerpt);
    
                //Set the excerpt word count and only break after sentence is complete.
                    $excerpt_word_count = 75;
                    $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 
                    $tokens = array();
                    $excerptOutput = '';
                    $count = 0;
    
                    // Divide the string into tokens; HTML tags, or words, followed by any whitespace
                    preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $pietergoosen_excerpt, $tokens);
    
                    foreach ($tokens[0] as $token) { 
    
                        if ($count >= $excerpt_word_count && preg_match('/[\?\.\!]\s*$/uS', $token)) { 
                        // Limit reached, continue until ? . or ! occur at the end
                            $excerptOutput .= trim($token);
                            break;
                        }
    
                        // Add words to complete sentence
                        $count++;
    
                        // Append what's left of the token
                        $excerptOutput .= $token;
                    }
    
                $pietergoosen_excerpt = trim(force_balance_tags($excerptOutput));
    
                    $excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&raquo;&nbsp;' . sprintf(__( 'Read more about: %s &nbsp;&raquo;', 'pietergoosen' ), get_the_title()) . '</a>'; 
                    $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end); 
    
                    $pos = strrpos($pietergoosen_excerpt, '</');
                    if ($pos !== false)
                    // Inside last HTML tag
                    $pietergoosen_excerpt = substr_replace($pietergoosen_excerpt, $excerpt_end, $pos, 0);
                    else
                    // After the content
                    $pietergoosen_excerpt .= $excerpt_end;
    
                return $pietergoosen_excerpt;   
    
            }
            return apply_filters('pietergoosen_custom_wp_trim_excerpt', $pietergoosen_excerpt, $raw_excerpt);
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题