如何限制wordpress中的摘录?

时间:2013-05-16 02:46:27

标签: php wordpress loops widget

我在我的网站上使用现代部落的活动日历,我想修改小部件。我有一切我想要的方式,除了一个小细节......摘录。这就是我的代码部分在我的网站中的外观,它提取了事件描述。但我不想要小部件的完整描述......我只有一个10个字左右,后面跟一个椭圆(...)。有什么想法吗?

<div class="entry-content tribe-events-event-entry" itemprop="description">
    <?php if (has_excerpt ()): ?>
        <?php the_excerpt(); ?>
    <?php else: ?>
        <?php the_content(); ?>
    <?php endif; ?>
</div> <!-- End tribe-events-event-entry -->

8 个答案:

答案 0 :(得分:4)

尝试将此添加到functions.php文件中。有关the_excerpt的更多信息,请访问:http://codex.wordpress.org/Function_Reference/the_excerpt

function custom_excerpt_length( $length ) {
return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

答案 1 :(得分:2)

您可以使用它来创建自定义摘录

// get the post content     
<?php $content = get_the_content(); ?>
<?php
// get the first 80 words from the content and added to the $abstract variable
 preg_match('/^([^.!?\s]*[\.!?\s]+){0,80}/', strip_tags($content), $abstract);
  // pregmatch will return an array and the first 80 chars will be in the first element 
  echo $abstract[0] . '...';
    ?>  

我从本教程中改编了这个 http://www.kavoir.com/2009/02/php-generating-summary-abstract-from-a-text-or-html-string-limiting-by-words-or-sentences.html 希望它有所帮助

答案 2 :(得分:1)

添加以下代码functions.php

function custom_excerpt_length( $length ) {
    return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

可以在此处找到the_excerpt上的MOre信息:http://www.happiweb.net/2014/05/gioi-han-tu-trong-mo-ta-theexcerpt-cua.html

答案 3 :(得分:0)

将此代码放在 function.php

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

然后

<?php the_content() ?> or <?php the_excerpt() ?>无论您将代码替换为

此: -

<?php echo word_count(get_the_excerpt(), '30'); ?>

答案 4 :(得分:0)

首先,不要在条件和()之间使用空格。 if (has_excerpt ()):应为if (has_excerpt()):

您只需使用

即可
function pietergoosen_custom_excerpts($limit) {
    return wp_trim_words(get_the_content(), $limit, '<a href="'. esc_url( get_permalink() ) . '">' . '&nbsp;&hellip;' . __( 'Read more &nbsp;&raquo;', 'pietergoosen' ) . '</a>');
}

并添加

<?php echo pietergoosen_custom_excerpts($limit); ?>

您需要在哪里显示摘录。只需将$limit替换为您想要返回的字词数。

返回40个单词的示例

<?php echo pietergoosen_custom_excerpts(40); ?>

答案 5 :(得分:0)

我刚刚找到了一个解决方案,在没有插件的情况下限制摘录中的单词数量。将以下代码添加到functions.php文件中。

<?php
// Custom Excerpt 
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;
}
?>

现在,不要在循环中使用the_content()或the_excerpt,而是使用excerpt($ limit)或content($ limit)。 如果您想将摘录限制为25个单词,则代码如下所示:

<?php echo excerpt(25); ?>

我得到了limiting the number of words in the excerpt here

的解决方案

答案 6 :(得分:0)

<?php 
$excerpt = get_the_excerpt();
$excerpt = substr( $excerpt , 0, 100); 
echo $excerpt;
?>

您可以通过更改100来将其更改为所需的数量。

答案 7 :(得分:-1)

你可以使用纯css并使用这种类型的代码在100px的文本结尾处获得省略号:

max-width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
相关问题