wordpress中的多段摘录长度 [英] Multiple excerpt lengths in wordpress

查看:79
本文介绍了wordpress中的多段摘录长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我明白你可以在functions.php中做到这一点:

 函数twentyten_excerpt_length($ length){
return 15;
}
add_filter('excerpt_length','twentyten_excerpt_length');

我想知道的是如何让多个数字返回不同的数值,这样我就可以获取侧栏循环的简短摘录,特色循环的更长摘录以及主要文章的最长摘录。



类似于在模板中使用这些内容:

 <?php the_excerpt('length-short')?> 
<?php the_excerpt('length-medium')?>
<?php the_excerpt('length-long')?>

干杯,
Dave

解决方案

如何...

 函数摘录($ limit){
$ excerpt = explode('',get_the_excerpt(),$ limit);

if(count($ excerpt)> = $ limit){
array_pop($ excerpt);
$ excerpt = implode(,$摘录)。 ...;
} else {
$ excerpt = implode(,$ excerpt);


$ excerpt = preg_replace('`\ [[^ \]] * \``,'',$ excerpt);

返回$摘录;
}

函数内容($ limit){
$ content = explode('',get_the_content(),$ limit);

if(count($ content)> = $ limit){
array_pop($ content);
$ content = implode(,$ content)。 ...;
} else {
$ content = implode(,$ content);
}

$ content = preg_replace('/ \ [。+ \] /','',$ content);
$ content = apply_filters('the_content',$ content);
$ content = str_replace(']]>',']]& gt;',$ content);

返回$ content;
}

然后在您刚刚使用的模板代码中。

 <?php echo excerpt(25); ?> 

from: http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your -expresspt-content-in-wordpress /


As it says in the title, I'm looking for multiple excerpt lengths in WordPress.

I understand you can do this in functions.php:

function twentyten_excerpt_length( $length ) {
    return 15;
}
add_filter( 'excerpt_length', 'twentyten_excerpt_length' );

What I want to know is how you can have multiple of these each returning different numerical values so I can get short excerpts for sidebar loops, longer excerpts for featured loops, and the longest excerpt for the main article.

Something like using these in the templates:

<?php the_excerpt('length-short') ?>
<?php the_excerpt('length-medium') ?>
<?php the_excerpt('length-long') ?>

Cheers, Dave

解决方案

How about...

function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);

      if (count($excerpt) >= $limit) {
          array_pop($excerpt);
          $excerpt = implode(" ", $excerpt) . '...';
      } else {
          $excerpt = implode(" ", $excerpt);
      }

      $excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt);

      return $excerpt;
}

function content($limit) {
    $content = explode(' ', get_the_content(), $limit);

    if (count($content) >= $limit) {
        array_pop($content);
        $content = implode(" ", $content) . '...';
    } else {
        $content = implode(" ", $content);
    }

    $content = preg_replace('/\[.+\]/','', $content);
    $content = apply_filters('the_content', $content); 
    $content = str_replace(']]>', ']]&gt;', $content);

    return $content;
}

then in your template code you just use..

<?php echo excerpt(25); ?>

from: http://bavotasan.com/tutorials/limiting-the-number-of-words-in-your-excerpt-or-content-in-wordpress/

这篇关于wordpress中的多段摘录长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆