Home » Functions » get_page_template_slug

Function Name: get_page_template_slug

In WordPress, the get_page_template_slug function is used to retrieve the slug of the template file that is assigned to a specific page. It returns the template slug as a string, or an empty string if no template is assigned to the page.

This function is useful when you need to programmatically determine the template being used for a specific page. You can use the returned template slug to perform conditional checks or modify the page output based on the template being used.

Example Usage:

Let’s say we have a custom post type called "products" and we want to display a different layout for a specific product page based on its assigned template. We can use the get_page_template_slug function to retrieve the template slug and then use it to conditionally load different template parts.

$product_id = get_the_ID(); // Get the ID of the current product
$template_slug = get_page_template_slug($product_id); // Get the template slug

if ($template_slug === 'product-template.php') {
    // Load custom template for product-template.php
    get_template_part('product', 'custom-layout');
} else {
    // Default template
    get_template_part('product', 'default-layout');

In this example, we retrieve the ID of the current product using the get_the_ID function. Then, we use the get_page_template_slug function to get the template slug associated with that product page. If the template slug matches ‘product-template.php’, we load a custom layout using the get_template_part function. Otherwise, we load the default layout.

By utilizing the get_page_template_slug function, we can dynamically adapt the layout and functionality of our WordPress website based on the assigned template for each page.

Learn More on

WordPress snippets using the get_page_template_slug function

Register an account to save your snippets or go Pro to get more features.