2

wordpress 中是否有一种本地方式可以将大写的第一个单词的第一个字母scentence / the_title的第一个字母大写?

如果没有,我如何在 php 中做到这一点,而它被包装在一个<a></a>标签中?

这是完整的代码行。

<a href="<?php the_permalink(); ?>"><?php ucfirst(the_title());?></a>

如您所见,我尝试过ucfirst,但它不起作用。我尝试的另一件事是:

<?php
$temp_title  = the_title();
$final_title = ucfirst($temp_title);
?>
<a href="<?php the_permalink(); ?>"><?php echo $final_title;?></a>
4

3 回答 3

4

ucfirst()返回一个字符串。您需要echo从返回的值ucfirst()

此外,WordPress 函数the_title()直接打印标题,而不是将其作为字符串返回。改为使用get_the_title()

<a href="<?php the_permalink(); ?>"><?php echo ucfirst(get_the_title());?></a>
于 2013-12-31T07:19:08.507 回答
2

我认为这是你的问题:http ://core.trac.wordpress.org/browser/tags/3.8/src/wp-includes/post-template.php#L51 。正如你所看到的,它the_title()正在使用get_the_title(),然后if($echo)它会呼应出来。我会试验和尝试get_the_title()

于 2013-12-31T07:41:41.830 回答
-1

首先,您的 echo $final_title 中有一个结尾括号没有匹配的括号。其次,您应该尝试将 ucfirst 应用于标题的回声。我删除了 $finaltitle,因为它不再有用。我还没有尝试过代码,但它应该可以工作。请注意,ucfirst() 仅在第一个字母在字母表中时才有效。

<?php
$temp_title  = the_title();
?>
<a href="<?php the_permalink(); ?>"><?php echo ucfirst($temp_title);?></a> 
于 2013-12-31T07:22:33.063 回答