WordPress.org

Ready to get started?Download WordPress

Forums

Exclude Pages by Page Template? (3 posts)

  1. ashes999
    Member
    Posted 8 years ago #

    My blog template displays all pages in the top, under my title, and such. I would like to exclude all pages of a template called Custom Works. How do I do it? I already created the template PHP file, and it prints the content just fine.

    I checked the wp_list_pages function, but it only excludes pages by ID.

    Better yet, if I can make the Custom Works template pages unlisted (don't appear anywhere unless you link them), that would also work.

  2. Kafkaesqui

    Posted 8 years ago #

    You can use this bit of code/sql query to 'discover' which Pages are assigned a specific template, and exclude those from your Page list (change value of $template to the filename of the Page template you need to match):

    <?php
    $template = 'custom-works.php';

    $Pages = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = '$template'");
    foreach($Pages as $Page) {
    $exclude_Pages .= $Page . ',';
    }

    wp_list_pages("exclude=$exclude_Pages");
    ?>

  3. ashes999
    Member
    Posted 8 years ago #

    Brilliant, thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags