I have a piece of code that outputs my post title but how can I output it as an H4?

Here is the code I have: <?php echo get_the_title( $post_id ); ?> Thanks

  • 1
    You can do: <h4><?php echo get_the_title( $post_id ); ?></h4> – Sally CJ Jan 26 at 21:05
  • Hi, thanks for your reply. I had already tried that and it did not work. – dan2017 Jan 26 at 21:07
  • Try the_title() as suggested in the answer. That way, though, you wouldn't be able to set the post ID the same way you could set it with get_the_title(). – Sally CJ Jan 26 at 21:19
  • @dan2017 when you say it didn't work, can you be more specific? What does the surrounding code look like? Does it display anything at all? Does it show the title without the h4 when you use your current code? And which file is this in? – Tom J Nowell Jan 26 at 21:22

Using your example, you could...

<h4><?php echo get_the_title( $post_id ); ?></h4>


$h4 = get_the_title();
echo '<h4>' . $h4 . '</h4>';

More on get_the_title().

Or you could use the_title().

the_title( '<h4>', '</h4>' );

The most simple way is:

<h4><?php echo get_the_title( $post_id ); ?></h4>

However, if you want check first if that post has a title and avoid an empty <h4> if it doesn't, you write:

$title = get_the_title( $post_id );

if ( $title )
    echo "<h4>$title</h4>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.