• Resolved mackie123

    (@mackie123)


    Hi,

    I was wondering if there is a function to generate a list of pages / links for all pages that use a certain template file?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Used this somewhere??? It lists all pages having a Page Template of the same value as the current page’s Template

    <?php
    $meta_key = '_wp_page_template';
    $template = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %d AND post_id = %s", $meta_key, $post->ID) );
    $template_pages = $wpdb->get_col( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %d AND meta_value = %s", $meta_key, $template) );
    wp_list_pages('include=' .implode(",", $template_pages));
    ?>

    Just set $template to the value of the Page Template you are looking for…

    Thread Starter mackie123

    (@mackie123)

    great thanks i will try it

    Thread Starter mackie123

    (@mackie123)

    Michael,

    I tried to use this code, but it still lists all of the pages for the site , not just from the included template.

    Hmm, thought I tested that, but I had a problem, so try this in your Page Template

    <?php
    $meta_key = '_wp_page_template';
    $template = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s AND post_id = %d", $meta_key, $post->ID) );
    $template_pages = $wpdb->get_col( $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = %s AND meta_value = %s", $meta_key, $template) );
    wp_list_pages('include=' .implode(",", $template_pages));
    ?>
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘list pages if certain template’ is closed to new replies.