How to Get the Current Page Template Name in WordPress

Current Page Template Name in WordPress

In WordPress, each page can have a specific template assigned to it. These templates control the layout and functionality of the page. Sometimes, you may need to retrieve the name of the template being used on the current page for various purposes, such as customization or troubleshooting. In this tutorial, we will guide you through the step-by-step process of obtaining the current page template name in WordPress.

Step 1:

Access the WordPress Theme Files To begin, you’ll need to access the theme files in your WordPress installation. Connect to your web server using FTP, or navigate to your WordPress directory using a file manager provided by your hosting provider.

Step 2:

Locate the Theme’s functions.php File Once you’ve accessed the theme files, locate the functions.php file within your active theme. This file contains the necessary hooks and functions to modify and enhance the functionality of your WordPress site.

Step 3:

Open the functions.php File Open the functions.php file using a text editor of your choice. This can be done by right-clicking on the file and selecting “Edit” or using the text editor provided by your FTP client.

Step 4:

Add a Function to Retrieve the Template Name Inside the functions.php file, add the following PHP code to create a function that will retrieve the template name for the current page:

function get_current_page_template_name() {
    $template = get_page_template_slug();
    $template = str_replace('.php', '', basename($template));
    return $template;
}

This code utilizes the get_page_template_slug() function to retrieve the template file name, removes the .php extension using str_replace(), and returns the template name.

Step 5:

Save the changes Save the modifications made to the functions.php file.

Step 6:

Display the Current Page Template Name To display the template name on the front end of your WordPress site, you can use the following code in any template file or within a shortcode:

$current_template = get_current_page_template_name();
echo "Current Page Template: " . $current_template;

This code calls the get_current_page_template_name() function we created earlier and displays the template name using echo.

Step 7:

Save and Update Template Files After adding the code snippet to display the current template name, save the template file (e.g., page.php, single.php) and update it to your WordPress installation. This will ensure that the changes take effect.

Step 8:

View the Current Page Template Name Now, visit your WordPress site and navigate to the page for which you want to retrieve the template name. You should see the current template name displayed on the front end of the page.

Conclusion:

Retrieving the current page template name in WordPress allows you to have better control over your website’s functionality and customization. By following the steps outlined in this tutorial, you can easily obtain the template name for any given page, helping you with further development or troubleshooting tasks.

Available For New Project

Abdullah Al Imran

I'm Abdullah Al Imran, a Full Stack WordPress Developer ready to take your website to the next level. Whether you're looking for WordPress Theme Development, adding new features, or fixing errors in your existing site, I've got you covered. Don't hesitate to reach out if you need assistance with WordPress, PHP, or JavaScript-related tasks. I'm available for new projects and excited to help you enhance your online presence. Let's chat and bring your website dreams to life!

Leave a Comment

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