Wordpress php摘录不起作用-v simple


Wordpress php excerpt not working - v simple

这里的第一篇文章,如果没有直接的问题,我不可能找到答案。我想,对于那些了解自己所看内容的人来说,这将是一个很容易回答的问题

我的主题中有一段摘录,但没有用。我的意思是,它显示了摘录,但它根本没有缩短单词的长度,它只是显示了整个事情。

在函数中创建摘录的代码:

Custom excerpt and more link

function ts_excerpt_more($more) {
    global $post;
    $ts_more = '<a href="'. get_permalink($post->ID) . '" class="btn">'. apply_filters('ts_more_text', __('Mas info', TS_DOMAIN)) .'</a>';
    return apply_filters('ts_more', $ts_more);
}
add_filter('excerpt_more', 'ts_excerpt_more');
function ts_the_excerpt($length = 55, $post_id = '', $more = ''){
    global $post, $more;
    if(!empty($post_id)) {
        $post = get_post($post_id);
        $more = false;
    }
    // respect excerpt_length filter
    $excerpt_length = apply_filters('excerpt_length', $length);
    $ts_more = ' <a href="'. get_permalink($post->ID) . '">['. apply_filters('ts_more_text', __('More info', TS_DOMAIN)) .'&hellip;]</a>';
    // when excerpt comes with custom more, set it
    // else use the default excerpt more
    $excerpt_more = (!empty($more)) ? $more : apply_filters('excerpt_more', $ts_more);
    if (strpos($post->post_content, '<!--more-->')) {
        $output = get_the_content('', true);
    } else {
        if(!empty($post->post_excerpt)) {   
            $output = $post->post_excerpt;
        } else {
            $content = strip_tags($post->post_content); 
            preg_match('/^'s*+(?:'S++'s*+){1,' . $excerpt_length . '}/', $content, $matches);     
            $output = $matches[0];
        }
    }
    $output = strip_shortcodes($output);
    $output = wpautop($output).wpautop($excerpt_more);
    echo $output;
}

以下是页面中的摘录:

<?php ts_the_excerpt(25, get_the_ID()); ?>

我真的不知道,我只能假设25是一个单词的限制,但这不起作用,任何帮助都将不胜感激。

Rob

您可以用更少的代码和更多的控制来实现这一点。

试着把它放在你的functions.php文件中

function limit_excerpt_length($string, $word_limit)
{
$words = explode(' ', $string);
return implode( ' ', array_slice($words, 0, $word_limit) );
}

把这个放在你想限制摘录的主题文件中。13是字数

    <?php echo limit_excerpt_length(get_the_excerpt(),  '13'); ?>