douangoo48863 2015-08-27 13:33
浏览 322
已采纳

博客摘录在wordpress中

I've purchased a theme and the blog excerpt works fine on their demo site, but not on my website's blog overview page. this is the code I found, but this is the wordpress post-template.php The theme does not have any blog settings which leads me to think it's a wordpress issues?

Hopre someone can help,

Many thanks!

P.S. Php newbie so please explain how I can fix this.

function get_the_excerpt( $deprecated = '' ) {
    if ( !empty( $deprecated ) )
        _deprecated_argument( __FUNCTION__, '2.3' );

    $post = get_post();
    if ( empty( $post ) ) {
        return '';
    }

    if ( post_password_required() ) {
        return __( 'There is no excerpt because this is a protected post.' );
    }

    /**
     * Filter the retrieved post excerpt.
     *
     * @since 1.2.0
     *
     * @param string $post_excerpt The post excerpt.
     */
    return apply_filters( 'get_the_excerpt', $post->post_excerpt );
}


/**
 * Whether post has excerpt.
 *
 * @since 2.3.0
 *
 * @param int|WP_Post $id Optional. Post ID or post object.
 * @return bool
 */
function has_excerpt( $id = 0 ) {
    $post = get_post( $id );
    return ( !empty( $post->post_excerpt ) );
}

/**
 * Display the classes for the post div.
 *
 * @since 2.7.0
 *
 * @param string|array $class One or more classes to add to the class list.
 * @param int|WP_Post $post_id Optional. Post ID or post object.
 */
function post_class( $class = '', $post_id = null ) {
    // Separates classes with a single space, collates classes for post DIV
    echo 'class="' . join( ' ', get_post_class( $class, $post_id ) ) . '"';
}
  • 写回答

1条回答 默认 最新

  • douke7431 2015-08-27 13:53
    关注

    By default the_excerpt() return the text in the excerpt field. Though you can use a filter to change what it returns:

    add_filter( 'get_the_excerpt', 'my_custom_excerpt' );
    

    It will trigger this function when the_excerpt() will be called:

    function my_custom_excerpt($excerpt) {
        if(empty($excerpt)) {
            return get_custom_excerpt();
        } else {
            return $excerpt;
        }
    }
    

    That way a custom excerpt will be used if the excerpt field is empty. Here is the code for get_custom_excerpt (it will do a "smart" ceasure):

    function print_excerpt($length)
    { 
        global $post;
        $text = $post->post_excerpt;
        if ( '' == $text ) {
                $text = get_the_content('');
                $text = apply_filters('the_content', $text);
                $text = str_replace(']]>', ']]>', $text);
        }
        $text = strip_shortcodes($text);
        $text = strip_tags($text, '<a>'); // change this to the tags you want to keep
    
        if (preg_match('/^.{1,'.$length.'}\b/s', $text, $match))
            $text = $match[0];
        $excerpt = reverse_strrchr($text, array('.','!'), 1, 100);
        if($excerpt) {
                echo apply_filters('the_excerpt',$excerpt);
        } else {
                echo apply_filters('the_excerpt',$text.'...');
        }
    }
    function reverse_strrchr($haystack, $needle, $trail, $offset=0)
    {
        return strrposa($haystack, $needle, $offset) ? substr($haystack, 0, strrposa($haystack, $needle, $offset) + $trail) : false;
    }
    // strrpos in Array mode
    function strrposa($haystack, $needles=array(), $offset=0)
    {
        if(!is_array($needles))
            $needles = array($needles);
        foreach($needles as $query) {
            if(strrpos($haystack, $query, $offset) !== false)
                return strrpos($haystack, $query, $offset);
        }
        return false;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题