2

Is there a way native way in wordpress to make uppercase the first letter of the first word of the scentence / the_title of the post?

If not, how do I do it in php, while it is wrapped in a <a></a> tag?

Here is the full line of code.

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

As you see, I have tried ucfirst, but it does not work. The other thing that I tried is:

<?php
$temp_title  = the_title();
$final_title = ucfirst($temp_title);
?>
<a href="<?php the_permalink(); ?>"><?php echo $final_title;?></a>
mickmackusa
  • 43,625
  • 12
  • 83
  • 136
gurung
  • 628
  • 2
  • 11
  • 33

3 Answers3

4

ucfirst() returns a string. You need to echo the value returned from ucfirst().

Furthermore, the WordPress function the_title() prints the title directly, rather than returning it as a string. Use get_the_title() instead.

<a href="<?php the_permalink(); ?>"><?php echo ucfirst(get_the_title());?></a>
200_success
  • 7,286
  • 1
  • 43
  • 74
  • yes, the parenthesis i put in here is wrong (i will just make the correction) but it is not in my actual code. My code is exactly as your alternative but it does not work. and yes i used your main solution `` but it does not work either. I appreciate your help anyways. – gurung Dec 31 '13 at 07:25
2

I think this is your problem: http://core.trac.wordpress.org/browser/tags/3.8/src/wp-includes/post-template.php#L51. As you can see the_title() is using get_the_title() and then if($echo) it echoes it out. I would experiment and try get_the_title().

200_success
  • 7,286
  • 1
  • 43
  • 74
-1

First off you had an ending parantheses that didn't have a matching parantheses in your echo $final_title. Second you should try to apply ucfirst to the echo of your title. I removed $finaltitle, because it no longer serves a purpose. I haven't tried the code, but it should work. Please note that ucfirst() only works if the first letter is in the alphabet.

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