MrThunder
MrThunder

Reputation: 745

Insert Wordpress title into html with php

I know this a simple question and I will probably get lambasted for it but I can not get this to work.

Currently the title prints before the h2 and the h2 is empty.

echo '<h2 class="profile-title">' . the_title() . '</h2>';

Thanks

Upvotes: 0

Views: 461

Answers (3)

Alex Andrei
Alex Andrei

Reputation: 7283

the_title() prints out the value by default.

You can return the value so you can use it in echo or print statements by passing true as the third argument, like this

echo '<h2 class="profile-title">' . the_title(null,null,true) . '</h2>';

or you can use the first and second parameters, before and after to build the title as you wish

the_title('<h2 class="profile-title">','</h2>');

or, if you still want to print it yourself

echo the_title('<h2 class="profile-title">','</h2>',true);

Upvotes: 0

Sanchit Gupta
Sanchit Gupta

Reputation: 3234

the_title() is use to print the title and you are printing it over and over by using echo. To print the title inside <h2> tag use it as :
echo '<h2 class="profile-title">' . get_the_title() . '</h2>';

Upvotes: 1

Marc B
Marc B

Reputation: 360872

Use get_the_title(). Standard rule in Wordpress: the_whatever() does OUTPUT immediately, while get_the_whatever() returns instead. So your the_title() outputs the title immediately, returns nothing, and then the rest of the echo kicks in.

Upvotes: 3

Related Questions