【问题标题】:Wordpress php excerpt not working - v simpleWordpress php摘录不起作用-简单
【发布时间】:2013-12-07 10:46:57
【问题描述】:

第一次在这里发帖,如果没有直接的问题,我不可能找到答案。我假设这对于了解他们在看什么的人来说很容易回答。

我的主题中有一段摘录,但它不起作用。我的意思是它显示了摘录,但它根本没有缩短字长,它只是显示了整个内容。

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

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 是一个字数限制,但这不起作用,任何帮助将不胜感激。

罗伯

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

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

    试着把它放在你的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'); ?>
    

    【讨论】:

    • 感谢您的帮助,不幸的是,这会导致在 if 循环中使用摘录的其他文件中出现问题:'
  • 好的,我意识到不工作的摘录在这里:'
  • 猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-07
    • 2023-03-16
    • 2013-10-13
    • 2014-01-11
    • 2016-12-23
    • 2016-04-09
    相关资源
    最近更新 更多