显示帖子摘录,而不是内容(如果可用

时间:2013-05-16 作者:Narga

如果可用,我计划显示帖子摘录而不是内容,如果不可用,则使用以下功能自动生成内容摘录:

<?php the_content( __( \'Continue reading <span class="meta-nav">&rarr;</span>\', \'blabla\' ) ); ?>
在中functions.php 我补充说:

# Function to trim the excerpt
if (!function_exists(\'narga_excerpts\')) :
    function narga_excerpts($content = false) {
        # If is the home page, an archive, or search results
            global $post;
            $excerpt_length = 40;
        # If an excerpt is set in the Optional Excerpt box
        if ( $post->post_excerpt ) {
        $content = the_excerpt();
        function custom_excerpt_length( $length ) {
            return 20;
        }
        add_filter( \'excerpt_length\', \'custom_excerpt_length\', 999 );
        function excerpt_readmore($more) {
        return \'... <a title="\' . the_title_attribute(\'echo=0\') . \'" href="\'. get_permalink($post->ID) . \'" class="more-link">  \' . __( \'Read more &#187;\', \'narga\' ) . \' </a>\';
        }
            add_filter(\'excerpt_more\', \'excerpt_readmore\');
        }
        # If no excerpt is set
        else {
            $content = $post->post_content;
            $words = explode(\' \', $content, $excerpt_length + 1);
            if(count($words) > $excerpt_length) {
                array_pop($words);
                $content = implode(\' \', $words);
            }
        }
        $content = \'<p>\' . $content . \'</p>\';
        # Make sure to return the content
        return $content;
    }
# Replace content with excerpt
add_filter(\'the_content\', \'narga_excerpts\');
endif;
但它不起作用。

1 个回复
SO网友:Charles Clarkson

将第9行更改为:

$content = get_the_excerpt();
the_excerpt() 与摘录相呼应。get_the_excerpt() 返回它。

结束