博客摘录在wordpress中

时间:2015-08-27 13:33:36

标签: php wordpress

我已经购买了一个主题,博客摘录在他们的演示网站上运行正常,但不在我的网站博客概述页面上。 这是我找到的代码,但这是wordpress post-template.php 该主题没有任何博客设置,这让我认为它是一个wordpress问题?

有人可以提供帮助,

非常感谢!

P.S。 Php newbie所以请解释我如何解决这个问题。

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 个答案:

答案 0 :(得分:0)

默认情况下the_excerpt()会返回摘录字段中的文字。虽然您可以使用过滤器来更改它返回的内容:

add_filter( 'get_the_excerpt', 'my_custom_excerpt' );

将在调用the_excerpt()时触发此功能:

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

这样,如果摘录字段为空,将使用自定义摘录。以下是get_custom_excerpt的代码(它将执行" 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;
}