0

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

4
  • 1
    You can do: <h4><?php echo get_the_title( $post_id ); ?></h4>
    – Sally CJ
    Commented Jan 26, 2019 at 21:05
  • Hi, thanks for your reply. I had already tried that and it did not work.
    – dan2017
    Commented Jan 26, 2019 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
    Commented Jan 26, 2019 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
    Commented Jan 26, 2019 at 21:22

2 Answers 2

0

Using your example, you could...

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

Or

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

More on get_the_title().

Or you could use the_title().

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

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 and acknowledge you have read our privacy policy.

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