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:
- 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. - Call the
wp_title()
functionInside the<title>
tags in your header file, you’ll call thewp_title()
function. You would usually find something like this in the header file:<title> <?php wp_title(); ?> </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:
<title> <?php wp_title( '|', true, 'right' ); bloginfo( 'name' ); ?> </title>
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.