Getting a post or page title in PHP is one of the most common tasks in WordPress development. Whether you’re building a custom theme, creating a loop, or generating meta tags, WordPress has built-in functions for retrieving titles. Here’s how to use get_the_title() and related title functions in WordPress.
Get the Current Post/Page Title
The get_the_title() function returns the title of the current post or page. It works with any post type, including custom post types. You can echo it directly or store it in a variable for further use.
| <?php | |
| // Display post title | |
| echo get_the_title(); |
This uses the global $post object, so it works inside the WordPress loop and in most template files.
Get a Title by Post ID
You can also pass a post ID to get_the_title() to retrieve the title of a specific post or page. This is useful inside custom loops or when you need the title of a post that isn’t the current one.
| <?php | |
| // Display post title using post ID | |
| echo get_the_title( 5 ); | |
| // Display post title and safely escape value | |
| echo esc_html( get_the_title() ); | |
| // Using the_title function to automatically echo the title, while you can append or prepend the title using the function as displayed below | |
| the_title( 'Before:', ' - After' ) |
If the post is password-protected or private, WordPress automatically prepends “Protected:” or “Private:” to the title returned by get_the_title().
the_title() vs get_the_title()
WordPress has two functions for post titles that beginners often confuse:
- get_the_title() returns the title as a string. You decide what to do with it (echo, store, modify).
- the_title() echoes the title directly. It also accepts optional
$beforeand$afterparameters to wrap the output in HTML.
Use get_the_title() when you need to manipulate the title before outputting it (truncating, escaping, concatenating). Use the_title() when you want to display it immediately with optional HTML wrapping:
// Wraps the title in an h1 tag and echoes it
the_title( '<h1>', '</h1>' );
// Returns the title so you can modify it
$title = get_the_title();
$short_title = wp_trim_words( $title, 5 );
In most cases, get_the_title() is the more flexible choice.
Sanitize a Title for HTML Attributes
If you’re using a title inside an HTML attribute (like a title="" tag or alt="" tag), use the_title_attribute() instead. It strips HTML tags and encodes special characters so quotation marks and other characters don’t break your markup.
// Safe for use in HTML attributes
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
Get the WordPress Site Title
If you need the site name (not a post title), use get_bloginfo(). This returns the site title set in Settings > General.
// Get the site title
$site_title = get_bloginfo( 'name' );
// Get the site tagline/description
$site_tagline = get_bloginfo( 'description' );
This is handy for custom headers, email templates, or anywhere you need to reference the site name dynamically.
Get the Archive Page Title
On category, tag, or custom taxonomy archive pages, get_the_title() won’t return the archive name. Use get_the_archive_title() instead:
// Returns something like "Category: Tutorials"
$archive_title = get_the_archive_title();
// For just the term name without the prefix
$archive_title = single_cat_title( '', false ); // category
$archive_title = single_tag_title( '', false ); // tag
$archive_title = single_term_title( '', false ); // any taxonomy
Those are several ways to get a title in WordPress using PHP. For related functions, see how to get a post ID, get the current page slug, or browse our WordPress code snippets.



8 Responses
Hello, Thank you for these explanations.
How can we retrieve the title of a custom_post_type by its ID?
Have a nice day
Just using get_the_title and putting your ID into it like this get_the_title(5313) will grab the title of a custom post type by its ID.
Thank you for your quick reply.
Great it works, thank you very much.
Small question, how can I dynamically retrieve the title ID if I decide to delete it and recreate another one from my plugin?
Here is his code:
add_action(‘init’, function () {
register_post_type(‘titlefooter’, [
‘label’ => ‘Footer titles’,
‘public’ => true,
‘menu_position’ => 3,
‘menu_icon’ => ‘dashicons-ellipsis’,
‘supports’ => [‘title’],
‘show_in_rest’ => true,
‘has_archive’ => true,
]);
});
I believe if the slug is still the same for the post type you’re registering it should act the same. I’m not sure of the question I guess.
No, sorry, that doesn’t work…
I will search again on the web hoping to find a solution for my Wordpress theme.
If not, I’ll think of another way to do it.
Thank you again for your help.
If you provide more details I am always around to help, feel free to email also [email protected]
Can also do like this:
global $post;
$post_title = $post->post_title;