0

我在我的网站上使用 Modern Tribe 的活动日历,我想修改小部件。我拥有我想要的一切,除了一个小细节......摘录。这就是我的代码部分在我的网站中的外观,它提取了事件描述。但我不想要小部件的完整描述...我只有一个 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 -->
4

8 回答 8

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 );
于 2013-05-16T02:50:44.803 回答
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 希望对你有帮助

于 2013-05-16T17:52:36.917 回答
1

将此代码放在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'); ?>
于 2014-04-08T11:57:47.150 回答
1

首先,不要在条件和之间使用空格()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); ?>

于 2014-04-08T15:18:15.670 回答
1

添加以下代码functions.php

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

更多关于 the_excerpt 的信息可以在这里找到:http ://www.happiweb.net/2014/05/gioi-han-tu-trong-mo-ta-theexcerpt-cua.html

于 2014-05-15T01:41:59.383 回答
1
<?php 
$excerpt = get_the_excerpt();
$excerpt = substr( $excerpt , 0, 100); 
echo $excerpt;
?>

您可以通过更改 100 将数量更改为您喜欢的值。

于 2019-09-16T12:40:43.547 回答
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); ?>

在这里找到了限制摘录中字数的解决方案

于 2015-01-23T10:01:15.927 回答
-2

您可以使用纯 css 并使用这种类型的代码在 100px 文本的末尾获取省略号:

max-width: 100px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
于 2013-05-16T02:58:08.333 回答