WordPress Dev: How can I get the current page name in WordPress?

Post author: Adam VanBuskirk
Adam VanBuskirk
5/13/23 in

In WordPress, you can use the get_the_title() function to get the title of the current page, no matter what file in the theme your code is running on.

Here’s an example:

$page_title = get_the_title();
echo $page_title;

This will output the title of the current page, which can be useful for a variety of purposes, such as displaying the page title in a custom template or using it as a variable in a custom function.

Note that this function only works inside the WordPress Loop, so you need to make sure that your code is running inside the loop before calling this function. If you need to get the title of a page outside the loop, you can use the get_post() function to retrieve the post object and then use the post_title property to get the title.

Here’s an example:

$post_id = get_the_ID();
$post = get_post($post_id);
$page_title = $post->post_title;
echo $page_title;

This will output the title of the current page, even if your code is running outside the loop.

Sign up today for our weekly newsletter about AI, SEO, and Entrepreneurship

Leave a Reply

Your email address will not be published. Required fields are marked *

Read Next

© 2024 Menyu LLC