Using WordPress ‘get_paged_template()’ PHP function

The get_paged_template() WordPress PHP function retrieves the path of the paged template in the current or parent template.

Usage

$template_path = get_paged_template();

Parameters

  • None

More information

See WordPress Developer Resources: get_paged_template()

Examples

Load paged template in a theme

This example loads the paged template in the active theme or parent theme.

$template_path = get_paged_template();
if ($template_path) {
    load_template($template_path);
} else {
    // Handle the case when the paged template is not found
}

This example displays the paged template path in the footer of a WordPress site.

function display_paged_template_path() {
    $template_path = get_paged_template();
    echo 'Paged Template Path: ' . $template_path;
}
add_action('wp_footer', 'display_paged_template_path');

Custom fallback for paged template

This example provides a custom fallback if the paged template is not found.

$template_path = get_paged_template();
if ($template_path) {
    load_template($template_path);
} else {
    // Fallback to a custom template
    get_template_part('template-parts/custom', 'paged');
}

Check if paged template exists

This example checks if the paged template exists in the active theme or parent theme.

function is_paged_template_available() {
    $template_path = get_paged_template();
    return $template_path ? true : false;
}

Modify paged template path

This example modifies the paged template path before loading it.

function modify_paged_template_path() {
    $template_path = get_paged_template();
    if ($template_path) {
        $template_path = str_replace('/templates/', '/custom-templates/', $template_path);
        load_template($template_path);
    }
}
add_action('template_redirect', 'modify_paged_template_path');