php Wordpress 标题:如果超过 50 个字符,则显示省略号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4617769/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:42:38  来源:igfitidea点击:

Wordpress Titles: If Longer Than 50 Characters, Show Ellipsis

phpwordpress

提问by Zach Smith

I have a wordpress site with titles, and if the title has more than 50 characters I need to add an ellipsis (...) at the end of the title and stop the title at 50 characters. Below is the PHP I am writing but it seems to not work correctly, seeking a PHP guru to teach me the correct way for this. Any help would be greatly appreciated.

我有一个带有标题的 wordpress 网站,如果标题超过 50 个字符,我需要在标题末尾添加一个省略号 (...) 并将标题停止在 50 个字符处。下面是我正在编写的 PHP,但它似乎无法正常工作,寻求 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 } ?>   

回答by Saul

The mb_strimwidthfunction does exactly that.

mb_strimwidth功能正是这么做的。

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

回答by Aamer Shahzad

WordPress has built in function "wp_trim_words()"to trim the sentences based on the number of words you provide,

WordPress 内置"wp_trim_words()"了根据您提供的单词数量修剪句子的功能,

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

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

to trim the title larger than 5 words you can do this

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

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

回答by Subroto Biswas

Single Code, 100% working

单一代码,100% 工作

PHP Function mb_strimwidth() | Wordpress Function get_the_title()

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

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

回答by Ali

Add this to your "functions.php" file in your theme folder....

将此添加到主题文件夹中的“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;
    }
}

then call the title like as follows

然后像下面这样调用标题

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

回答by Johan Wallberg

<?php 
$title  = the_title('','',false);
if(strlen($title) > 60):
    echo trim(substr($title, 0, 65)).'...';
else:
    echo $title;
endif;
?>

回答by NickAldwin

You're checking the length of the string "the_title()". Remove the quotes, and it will probably work (I'm not 100% sure of the difference between the_title() and get_the_title(), as I haven't used Wordpress in a while -- you might have to switch that around too):

您正在检查 string 的长度"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 } ?>   

or maybe

或者可能

<?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 } ?>   

回答by Aaron Hathaway

Take the_title()out of quotes when using the strlen()function.

the_title()使用时的报价出来strlen()的功能。

回答by Brad

echo (strlen(the_title())>50) ? (substr(the_title(), 0, 50) . "...") : the_title());

This is a ternary operator. What it basically says is if the result from the_title()is more than 50 characters, then echo the first 50 characters and then the string .... Otherwise, just echo the result from the_title().

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

You can read more about substrhere: http://php.net/manual/en/function.substr.php

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

You can find info on the ternary operator here: http://php.net/manual/en/language.operators.comparison.php

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