$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Print post title with markup|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Print post title with markup

matteradmin6PV0评论

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>";

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far