最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Exclude pages with certain template from wp_list_pages

programmeradmin1浏览0评论

Before you mark this as duplicate I have tried every method in all the other questions and non of them have worked.

I am trying to exclude any page with the page template page-noindex.php from the wp_list_pages(); query.

The below code does not work and when I echo out $the_query it just displays 'Array'.

    <?php 
        $the_query = array(
            'post_type'  => 'page',  /* overrides default 'post' */
            'meta_key'   => '_wp_page_template',
            'meta_value' => 'page-templates/page-noindex.php'
        );

        $args = array(
            'exclude'      => $the_query,
            'title_li'     => '',
            'sort_column'  => 'menu_order, post_title',
            'post_type'    => 'page',
                'post_status'  => 'publish' 
        ); ?>

    <?php wp_list_pages($args) ?>

Before you mark this as duplicate I have tried every method in all the other questions and non of them have worked.

I am trying to exclude any page with the page template page-noindex.php from the wp_list_pages(); query.

The below code does not work and when I echo out $the_query it just displays 'Array'.

    <?php 
        $the_query = array(
            'post_type'  => 'page',  /* overrides default 'post' */
            'meta_key'   => '_wp_page_template',
            'meta_value' => 'page-templates/page-noindex.php'
        );

        $args = array(
            'exclude'      => $the_query,
            'title_li'     => '',
            'sort_column'  => 'menu_order, post_title',
            'post_type'    => 'page',
                'post_status'  => 'publish' 
        ); ?>

    <?php wp_list_pages($args) ?>
Share Improve this question asked Aug 21, 2017 at 20:56 Daniel VickersDaniel Vickers 1185 bronze badges 1
  • 1 Exclude does not accept an array. It only accepts a comma separated list of page IDs. Also, you can not echo an array. You should use print_r($the_query) instead. – Johansson Commented Aug 21, 2017 at 21:08
Add a comment  | 

1 Answer 1

Reset to default 1

Daniel, exclude parameter doesn't accept array.

Use your code this way:

$exclude = [];
foreach(get_pages(['meta_key' => '_wp_page_template', 'meta_value' => 'page-templates/page-noindex.php']) as $page) {
    $exclude[] = $page->post_id;
}

$args = array(
    'exclude'      => implode(",", $exclude),
    'title_li'     => '',
    'sort_column'  => 'menu_order, post_title',
    'post_type'    => 'page',
    'post_status'  => 'publish'
); 
wp_list_pages($args);

I think you can refactor it better for your needs

发布评论

评论列表(0)

  1. 暂无评论