How to Get Page Title in WordPress?

To retrieve the page title in WordPress, you can use the built-in WordPress function wp_title(). This function fetches the title for a page which is set in the page’s metadata.

Here is how you can use it:

  1. Open the appropriate fileFind and open the PHP file where you want to display the page title. This will typically be in your theme’s header.php file, because the title is usually displayed in the header of a webpage.
  2. Call the wp_title() functionInside the <title> tags in your header file, you’ll call the wp_title() function. You would usually find something like this in the header file:
        <?php wp_title(); ?>

    This will output the title of the current page.

Remember, if you want to add a separator and a description (like the blog name) to the title, you can pass parameters to the wp_title() function. For instance:

    <?php wp_title( '|', true, 'right' ); bloginfo( 'name' ); ?>

In the above code, ‘|’ is the separator and ‘right’ means the separator will be placed to the right of the title. The bloginfo( 'name' ) function adds the blog’s name after the separator.

Always be careful when editing PHP files in WordPress, as a mistake can take your site down. Always keep a backup of your files before making changes.

Leave a Comment