How to use the_title() function in WordPress

Keeping track of the titles of the pages and posts on your website is an essential part of designing a website. The the_title() function is extremely helpful in situations like these. In this article, we will discuss what the the_title() function is, how it should be used, and some of the key features that it possesses that can help improve the usability of your website.

What is the_title() function?

The the_title() function is a built-in WordPress function that retrieves and displays the title of a particular post or page. It is an essential part of WordPress templates because it enables users to generate dynamic titles that are optimised for search engines and that can be made to automatically update whenever new content is added.

How to use the_title() function

Using the_title() function is quite simple. All you need to do is place it within the loop of your WordPress template file. Here’s a basic example of how to use it:

<?php if (have_posts()) : ?>
  <?php while (have_posts()) : the_post(); ?>
      <?php the_title(); ?>
  <?php endwhile; ?>
<?php endif; ?>

In the example above, the_title() function is called within an <h1> tag, which ensures that the title of each post or page is displayed as an H1 heading.

Customising the_title() function

The the_title() function has three optional parameters that allow you to further customize its output:

  • $before (string): HTML or text that is displayed before the title.
  • $after (string): HTML or text that is displayed after the title.
  • $echo (bool): Whether to display the title (true) or return it for use in PHP (false).

Here’s an example of how to use these parameters:

<?php the_title('<h1 class="post-title">', '</h1>', true); ?>

In this example, we’ve added a CSS class called “post-title” to the <h1> tag and specified that the title should be displayed (echo = true).

Conditional display of titles

In some cases, you may want to display a custom title or text if the post or page doesn’t have a title. You can achieve this using the if/else statement in conjunction with the_title() function:

<?php if (get_the_title()) : ?>
    <?php the_title(‘<h1>’,’</h1>’); ?>
<?php else : ?>
  <h1>No Title</h1>
<?php endif; ?>


When it comes to managing titles in your WordPress templates, the the_title() function is an indispensable tool to have. You’ll be able to generate dynamic, SEO-friendly titles that improve the overall user experience on your website if you understand how to use the tool and how to customise the output it generates. Don’t be afraid to try out different things with this highly adaptable function in order to get the optimal outcomes for your particular requirements.