【问题标题】:How to limit an excerpt in wordpress?如何限制wordpress中的摘录?
【发布时间】:2013-05-16 02:46:27
【问题描述】:

我在我的网站上使用 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 -->

【问题讨论】:

    标签: php wordpress loops widget


    【解决方案1】:

    尝试将此添加到您的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 );
    

    【讨论】:

    • 感谢您的建议。我唯一的后续问题是我把它放在哪里?
    • 将其添加到您主题中的 functions.php 文件中。 wp-content/yourthemename/functions.php
    • 我在我网站的其他地方使用了摘录,所以我不想完全修改它。这会那样做吗?
    • 是的,它会这样做。另一种解决方案是在 Wordpress 帖子中手动输入摘录。如果文本框不可见,请单击右上角的屏幕选项并确认已选中摘录。
    【解决方案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 希望对你有帮助

    【讨论】:

    • 工作就像一个魅力。我刚刚用您拥有的内容替换了摘录/内容调用,效果很好!谢谢!
    【解决方案3】:

    把这段代码放在function.php

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

    然后

    &lt;?php the_content() ?&gt; or &lt;?php the_excerpt() ?&gt; 将代码替换为任何内容

    这个:-

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

    【讨论】:

      【解决方案4】:

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

      并添加

      &lt;?php echo pietergoosen_custom_excerpts($limit); ?&gt;

      您需要显示摘录的位置。只需将$limit 替换为您想要返回的字数即可。

      返回 40 个单词的示例

      &lt;?php echo pietergoosen_custom_excerpts(40); ?&gt;

      【讨论】:

        【解决方案5】:

        添加如下代码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

        【讨论】:

          【解决方案6】:
          <?php 
          $excerpt = get_the_excerpt();
          $excerpt = substr( $excerpt , 0, 100); 
          echo $excerpt;
          ?>
          

          您可以通过更改 100 将金额更改为您喜欢的值。

          【讨论】:

            【解决方案7】:

            我刚刚找到了一个解决方案,可以在没有插件的情况下限制摘录中的单词数。将以下代码添加到您的 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的解决方案

            【讨论】:

              【解决方案8】:

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

              max-width: 100px;
              white-space: nowrap;
              overflow: hidden;
              text-overflow: ellipsis;
              

              【讨论】:

              • 有趣...不知道这是可能的。它就像一个魅力,除了省略号。有什么想法吗?
              • 'code' li.tribe_events .entry-content { 宽度:100%;空白:nowrap;溢出:隐藏;文本溢出:省略号; } '代码'
              猜你喜欢
              • 1970-01-01
              • 2014-04-12
              • 2018-12-30
              • 1970-01-01
              • 1970-01-01
              • 2014-07-01
              • 1970-01-01
              • 2014-10-24
              • 1970-01-01
              相关资源
              最近更新 更多