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:
<?php
$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:
<?php
$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.