In WordPress, you can get the current page name using various methods, depending on the context and the information you need. Here are a few ways to get the current page name:
Get the current page’s title:
$current_page_title = get_the_title();
Get the current page’s slug:
global $post; $current_page_slug = $post->post_name;
Get the current page’s ID, and then use it to get the title or the slug:
$current_page_id = get_queried_object_id(); // To get the current page title $current_page_title = get_the_title($current_page_id); // To get the current page slug $current_page_object = get_post($current_page_id); $current_page_slug = $current_page_object->post_name;
If you’re working within the loop:
if (have_posts()) : while (have_posts()) : the_post(); $current_page_title = get_the_title(); $current_page_slug = $post->post_name; endwhile; endif;
These methods can be used in your theme files, such as header.php, footer.php, or any other template file where you need to get the current page name.
Keep in mind that the $post variable should be available in the scope you’re using these methods. If you face any issues, you may need to declare global $post; before using it.