WordPress:仅回显 post 标题的前 60 个字符
WordPress: echoing just the first 60characters of the post title
我正在设计一个 WordPress 主题,我想确保如果 post 标题超过 60 个字符,它会显示前 6ß0 个字符 +
最后的三点 (...)
原生 Php 想要:
<?php
if (strlen($title) <= 60) {
echo %title
} else {
echo (substr($title, 60) . "..."
}
?>
我的问题是,在 WordPress 中,变量的语法不是 $title,而是 %title,正如您在代码中看到的那样:
<?php previous_post_link( '%link', '%title ' ); ?>
我的问题是:
- WordPress 中的最终 IF 是怎样的
- shorthandif/else(三元)形式如何?
谢谢
您可以通过创建自定义 post_nav 函数来实现此目的
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if ($prev_post)
{
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
if (strlen($prev_title) >= 60) //<-- here is your custom checking
{
$prev_title = (substr($prev_title, 0, 60)) . "...";
}
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title . '" class=" "><strong><<< "' . $prev_title . '"</strong></a>' . "\n";
}
?>
</div>
<div class="next-posts pull-right">
<?php
$next_post = get_next_post();
if ($next_post)
{
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
if (strlen($next_title) >= 60) //<-- here is your custom checking
{
$next_title = (substr($next_title, 0, 60)) . "...";
}
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title . '" class=" "><strong>"' . $next_title . '" >>></strong></a>' . "\n";
}
?>
</div>
希望对您有所帮助!
我正在设计一个 WordPress 主题,我想确保如果 post 标题超过 60 个字符,它会显示前 6ß0 个字符 + 最后的三点 (...)
原生 Php 想要:
<?php
if (strlen($title) <= 60) {
echo %title
} else {
echo (substr($title, 60) . "..."
}
?>
我的问题是,在 WordPress 中,变量的语法不是 $title,而是 %title,正如您在代码中看到的那样:
<?php previous_post_link( '%link', '%title ' ); ?>
我的问题是:
- WordPress 中的最终 IF 是怎样的
- shorthandif/else(三元)形式如何?
谢谢
您可以通过创建自定义 post_nav 函数来实现此目的
<div class="prev-posts pull-left">
<?php
$prev_post = get_previous_post();
if ($prev_post)
{
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
if (strlen($prev_title) >= 60) //<-- here is your custom checking
{
$prev_title = (substr($prev_title, 0, 60)) . "...";
}
echo "\t" . '<a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title . '" class=" "><strong><<< "' . $prev_title . '"</strong></a>' . "\n";
}
?>
</div>
<div class="next-posts pull-right">
<?php
$next_post = get_next_post();
if ($next_post)
{
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
if (strlen($next_title) >= 60) //<-- here is your custom checking
{
$next_title = (substr($next_title, 0, 60)) . "...";
}
echo "\t" . '<a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title . '" class=" "><strong>"' . $next_title . '" >>></strong></a>' . "\n";
}
?>
</div>
希望对您有所帮助!