The get_the_title(). function retrieves the title of a post or page in the loop. Here’s a simple example:
$post_title = get_the_title( $post_id );
echo $post_title;
If you’re working with custom post types, you can retrieve their titles by passing the post ID:
$post_id = 42;
$title = get_the_title( $post_id );
echo “The title is: ” . $title;
If a post has no title, you can provide a fallback:
$title = get_the_title( $post_id );
if (empty($title)) {
$title = “Untitled Post”;
}
echo $title;
get_the_title() returns the title as a string.
the_title() echoes the title directly to the page.
Use get_the_title() when you need to manipulate the title before output.
Use the_title() when you simply want to display the title in your theme.
If you’re working outside The Loop, use the global $post object:
global $post;
echo get_the_title($post->ID);
To get titles from a custom query:
$args = array(‘post_type’ => ‘post’, ‘posts_per_page’ => 5);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
echo get_the_title();
}
}
wp_reset_postdata();
You can modify titles using filters:
add_filter(‘the_title’, function($title) {
return strtoupper($title);
});
You can append or prepend text to titles dynamically:
add_filter(‘the_title’, function($title, $id) {
return “Important: ” . $title;
}, 10, 2);
For custom post types, ensure that the title field is enabled:
register_post_type(‘portfolio’, array(
‘supports’ => array(‘title’, ‘editor’, ‘thumbnail’)
));
Using plugins like Yoast SEO or All in One SEO Pack, you can set custom SEO titles that override the default WordPress title settings.
Keep titles concise and descriptive.
Include primary keywords naturally.
Use actionable words to increase clicks.
Popular plugins like Yoast SEO allow you to:
Set custom meta titles.
Add focus keywords.
Generate automated title tags for consistency.
Using schema markup helps search engines understand your content better:
<script type="application/ld+json">
{"@context": "https://schema.org","@type": "Article","headline": "How to Get the Title in WordPress"
}
</script>
If your title isn’t showing, check that the_title() or get_the_title() is inside The Loop.
Check your SEO plugin settings.
Ensure titles are properly structured in your theme.
Use var_dump(get_the_title($post_id)) to debug title issues.
Understanding how to retrieve and manipulate titles in WordPress is essential for developers and content creators alike. By using functions like get_the_title() and the_title(), optimising titles for SEO, and troubleshooting common issues, you can ensure that your WordPress titles are effective and engaging.
get_the_title() returns the title as a string, while the_title() outputs it directly to the page.
Use global $post; or get_the_title($post_id); to fetch titles outside The Loop.
Check if the title function is inside The Loop and ensure SEO plugins aren’t overriding it.
Use the the_title filter hook to change titles dynamically before they’re displayed.
Write concise, keyword-rich titles and use SEO plugins like Yoast to customise them effectively.
By mastering these techniques, you can enhance both your WordPress development skills and your site’s SEO performance.