When you’re creating special features for your WordPress website, you might need to get the title of a post or page. Fortunately, WordPress makes it easy to do this using a built-in function called `get_the_title()`. This function helps you fetch the title of the current post or page using PHP. It’s handy when you want to show a post’s title in a custom section of your website’s design.
In this guide, I’ll show you how to use the `get_the_title()` function in WordPress.
How to Display the Page/Post Title in PHP
Now, let’s explore how to use the `get_the_title()` function to show the title of a page or post on your WordPress site using PHP. This is valuable when you’re crafting your own theme or custom code. The great thing is that this function can fetch the title of various types of content, such as pages and posts. It even works for custom post types you might add to your website.
You can easily use `echo get_the_title()` to display the title of the current page or post. This approach usually works well and is likely what you’re looking for in most situations.
<?php // Display post title echo get_the_title();
On the other hand, you can also specify a particular post’s ID within the `get_the_title` function to retrieve that specific post’s title. In the following examples, you can see how I’m using the function to obtain the title of a post or page based on its ID. This method is particularly useful if you’ve crafted a custom loop and wish to obtain the title in that manner.
<?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' );
I’ve also provided an example of using the `the_title()` function, which displays the title without needing to use “echo.” The `the_title()` function also lets you include content before and after the title, as shown in the previous example. However, in most cases, it’s recommended to use `get_the_title()` because it allows you to modify the title string if necessary.
When you use the `get_the_title()` function on a password-protected or private post, the words “Protected” and “Private” will be added before the post title.
If you’re using the title for an HTML tag’s title attribute, you can also use the `the_title_attribute()` function. This function echoes a sanitized version of the title, which prevents issues like quotation marks from disrupting your layout when the title is used within an HTML attribute.
How can retrieve the title of a custom_post_type by its ID?
To get the title of a custom post type by its ID in WordPress, you can use the get_the_title() function. Here’s how you can do it
<?php $post_id = 12; // Replace 123 with the actual ID of your custom post type $title = get_the_title($post_id); echo $title;
I hope this article has helped you understand how to retrieve the title of a page in WordPress using PHP.
If you have any questions about working with WordPress, feel free to ask in the comments section below!
Related Articles
Leave a Reply