php - Exclude pages with certain template from wp_list_pages

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

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745554717a4632749.html

相关推荐

  • php - Exclude pages with certain template from wp_list_pages

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

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信