Print post title with markup

admin2025-06-04  2

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

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

Share Improve this question edited Jan 26, 2019 at 21:06 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jan 26, 2019 at 20:57 dan2017dan2017 31 silver badge4 bronze badges 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
Add a comment  | 

2 Answers 2

Reset to default 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>' );

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>";
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748972527a315281.html

最新回复(0)