首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wordpress博客摘录

wordpress博客摘录
EN

Stack Overflow用户
提问于 2015-08-27 13:33:35
回答 1查看 579关注 0票数 0

我已经购买了一个主题和博客摘录在他们的演示网站上很好,但不是在我的网站的博客概述页面。这是我找到的代码,但这是wordpress的Posttemplate.php主题没有任何博客设置,这让我认为这是wordpress的问题?

希望有人能帮忙

非常感谢!

Php新手,所以请解释我如何解决这个问题。

代码语言:javascript
复制
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 ) ) . '"';
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-27 13:53:48

默认情况下,the_excerpt()返回摘录字段中的文本。尽管可以使用筛选器更改它返回的内容:

代码语言:javascript
复制
add_filter( 'get_the_excerpt', 'my_custom_excerpt' );

当调用the_excerpt()时,它将触发此函数:

代码语言:javascript
复制
function my_custom_excerpt($excerpt) {
    if(empty($excerpt)) {
        return get_custom_excerpt();
    } else {
        return $excerpt;
    }
}

这样,如果摘录字段为空,则将使用自定义摘录。下面是get_custom_excerpt的代码(它将执行“智能”停止):

代码语言:javascript
复制
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;
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32250833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档