WordPress.org

Forums

List pages that belong to certain template (3 posts)

  1. elianab
    Member
    Posted 6 years ago #

    I am making a website that has 2 sections, and each uses a different template. I want the sidebar of each section to list all of the pages that use that particular template. I could manually include or exclude some pages, but then if I add a page I'll have to change the code. Is there a way to tell it to list all pages belonging to a certain template?

  2. MichaelH
    Member
    Posted 6 years ago #

    Something like this?

    $template_pages = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = 'mypagetemplate'");
    wp_list_pages('exclude=' .implode(",", $template_pages);

    of course replace mypagetemplate with your value.

  3. MichaelH
    Member
    Posted 6 years ago #

    Oops, change the exclude to include. Sorry.

    $template_pages = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = 'mypagetemplate'");
    wp_list_pages('include=' .implode(",", $template_pages);

Topic Closed

This topic has been closed to new replies.

About this Topic