【问题标题】:Wordpress Titles: If Longer Than 50 Characters, Show EllipsisWordpress 标题:如果超过 50 个字符,则显示省略号
【发布时间】:2011-01-06 17:29:07
【问题描述】:

我有一个带有标题的 WordPress 网站,如果标题超过 50 个字符,我需要在标题末尾添加一个省略号 (...),并将标题停止在 50 个字符处。

以下是我正在编写的 PHP,但它似乎无法正常工作。

<?php if (strlen("the_title()") > 50) { ?>
    <?php the_title(); ?>
<?php } if (strlen("the_title()") < 50) { ?>
    <?php echo substr(get_the_title(), 0, 50); ?>...
<?php } ?>   

【问题讨论】:

  • 您期望它没有做什么?此外,删除所有使用 the_title() 的引号。你想要返回值,而不是那个字符串。

标签: php wordpress


【解决方案1】:

mb_strimwidth 函数正是这样做的。

echo mb_strimwidth(get_the_title(), 0, 50, '...');

【讨论】:

  • +1,既然我们已经有了轮子,为什么还要重新发明轮子?
  • 如何将wordpress标题放入变量中?
  • @Saul,很好的答案!我喜欢它。
  • @HollerTrain $title = get_the_title();
  • @Saul 哇,我不知道这存在。谢谢,现在我知道了! :)
【解决方案2】:

WordPress 内置了"wp_trim_words()" 功能,可以根据您提供的字数修剪句子,如果您想按字数修剪,那么此功能可能会对您有所帮助。

https://codex.wordpress.org/Function_Reference/wp_trim_words

要修剪超过 5 个单词的标题,您可以这样做

<?php
$title = get_the_title();
$short_title = wp_trim_words( $title, 5, '...' );
echo '<h3>'.$short_title.'</h3>';
?>

【讨论】:

  • 这正是我所需要的。谢谢
  • OP要求限制字符,但我认为这个答案非常有帮助,正是我想要的
  • 是否可以仅在移动视图中执行此操作?如果是这样的话?代码是什么样的?
【解决方案3】:

单一代码,100% 工作

PHP 函数 mb_strimwidth() | Wordpress 函数 get_the_title()

<?php 
echo mb_strimwidth( get_the_title(), 0, 100, '...' ); 
?>

【讨论】:

    【解决方案4】:

    将此添加到主题文件夹中的“functions.php”文件中......

    function the_title_excerpt($before = '', $after = '', $echo = true, $length = false) 
      {
        $title = get_the_title();
    
        if ( $length && is_numeric($length) ) {
            $title = substr( $title, 0, $length );
        }
    
        if ( strlen($title)> 0 ) {
            $title = apply_filters('the_title_excerpt', $before . $title . $after, $before, $after);
            if ( $echo )
                echo $title;
            else
                return $title;
        }
    }
    

    然后像下面这样调用标题

    <?php the_title_excerpt('', '...', true, '50'); ?>
    

    【讨论】:

      【解决方案5】:

      您正在检查字符串 "the_title()" 的长度。删除引号,它可能会起作用(我不是 100% 确定 the_title() 和 get_the_title() 之间的区别,因为我有一段时间没有使用过 Wordpress - 你可能也需要切换它) :

      <?php if (strlen(the_title()) > 50) { ?>
                      <?php the_title(); ?>
                  <?php } if (strlen(the_title()) < 50) { ?>
                      <?php echo substr(get_the_title(), 0, 50); ?>...
                  <?php } ?>   
      

      或许

      <?php if (strlen(get_the_title()) > 50) { ?>
                      <?php the_title(); ?>
                  <?php } if (strlen(get_the_title()) < 50) { ?>
                      <?php echo substr(get_the_title(), 0, 50); ?>...
                  <?php } ?>   
      

      【讨论】:

      • 第二个似乎有效。你知道可以使用更好的代码来代替我正在使用的这个基本的if if吗?
      • @HollerTrain 是的......使用扫罗的回答:)。我发布了一条回复,向您展示了如何使用该变量。
      【解决方案6】:
      <?php 
      $title  = the_title('','',false);
      if(strlen($title) > 60):
          echo trim(substr($title, 0, 65)).'...';
      else:
          echo $title;
      endif;
      ?>
      

      【讨论】:

        【解决方案7】:

        使用strlen() 函数时,将the_title() 去掉引号。

        【讨论】:

          【解决方案8】:
          echo (strlen(the_title())>50) ? (substr(the_title(), 0, 50) . "...") : the_title());
          

          这是一个三元运算符。它的基本意思是如果the_title() 的结果超过50 个字符,则回显前50 个字符,然后回显字符串...。否则,只需回显来自the_title() 的结果。

          您可以在此处阅读有关substr 的更多信息:http://php.net/manual/en/function.substr.php

          您可以在此处找到有关三元运算符的信息:http://php.net/manual/en/language.operators.comparison.php

          【讨论】:

          • 好吧,标题在哪里?我面前没有你的 wordpress 安装。我会假设函数the_title() 会返回一个包含标题的字符串。不是吗?
          • @Brad 我认为the_title() 格式化标题,而get_the_title() 返回标题字符串。但我不是 Wordpress 专家。
          • 嗯,在这种情况下,HollerTrain,只需将上面写着the_title() 的所有内容替换为get_the_title(),但您应该真正遵循 Saul 的回答。这样更好。
          • 如果你使用 strlen 你必须使用get_the_title 我相信。无论如何,我把你的代码放在 index.php 中,我得到一个白屏,所以似乎代码正在破坏网站。不过,请您帮忙。
          • 我用 get_the_title() 替换了所有内容,但仍然无法正常工作
          【解决方案9】:

          使用'strlen'

          eg: 
          
            <?php echo ((strlen(get_the_title())>50) ? (substr(get_the_title(), 0, 50) . "...") : get_the_title())?>
          

          【讨论】:

            猜你喜欢
            • 2021-05-19
            • 1970-01-01
            • 2019-06-19
            • 2015-03-26
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-08-20
            • 2016-11-20
            相关资源
            最近更新 更多