可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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.
<?php if (strlen("the_title()") > 50) { ?>
<?php the_title(); ?>
<?php } if (strlen("the_title()") < 50) { ?>
<?php echo substr(get_the_title(), 0, 50); ?>...
<?php } ?>
回答1:
The mb_strimwidth function does exactly that.
echo mb_strimwidth(get_the_title(), 0, 50, '...');
回答2:
WordPress has built in function "wp_trim_words()"
to trim the sentences based on the number of words you provide,
https://codex.wordpress.org/Function_Reference/wp_trim_words
to trim the title larger than 5 words you can do this
<?php
$title = get_the_title();
$short_title = wp_trim_words( $title, 5, '...' );
echo '<h3>'.$short_title.'</h3>';
?>
回答3:
Single Code, 100% working
PHP Function mb_strimwidth() | Wordpress Function get_the_title()
<?php
echo mb_strimwidth( get_the_title(), 0, 100, '...' );
?>
回答4:
Add this to your "functions.php" file in your theme folder....
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'); ?>
回答5:
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):
<?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 } ?>
回答6:
<?php
$title = the_title('','',false);
if(strlen($title) > 60):
echo trim(substr($title, 0, 65)).'...';
else:
echo $title;
endif;
?>
回答7:
Take the_title()
out of quotes when using the strlen()
function.
回答8:
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()
.
You can read more about substr
here: 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