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

wp list pages - Exclude certain template from wp_list_pages

programmeradmin1浏览0评论

I'm trying to do something similar to the question in Exclude pages with certain template from wp_list_pages, but the $exclude variable doesn't seem to be returning the _wp_page_template value to then filter out the relevant pages with the template guidance-note-template.php from the list. Any tips, greatly appreciated.

<?php

global $post;

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

$children = get_pages( array( 'child_of' => $post->ID ) );
$hasChild = (count( $children ) > 0 );
$page_id = ($hasChild) ? $post->ID : wp_get_post_parent_id( $post->ID );
    wp_list_pages( array(
    'title_li'    => '',
    'child_of'    => $page_id,
    'exclude'      => implode(",", $exclude),
));
?>

I'm trying to do something similar to the question in Exclude pages with certain template from wp_list_pages, but the $exclude variable doesn't seem to be returning the _wp_page_template value to then filter out the relevant pages with the template guidance-note-template.php from the list. Any tips, greatly appreciated.

<?php

global $post;

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

$children = get_pages( array( 'child_of' => $post->ID ) );
$hasChild = (count( $children ) > 0 );
$page_id = ($hasChild) ? $post->ID : wp_get_post_parent_id( $post->ID );
    wp_list_pages( array(
    'title_li'    => '',
    'child_of'    => $page_id,
    'exclude'      => implode(",", $exclude),
));
?>
Share Improve this question asked Apr 26, 2019 at 11:44 SketchybearSketchybear 233 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For children pages to show, the 'hierarchical' => 0 option must also be set in your get_pages() function.

I copied your code and initially had the same results (it returned no pages). Adding in the hierarchical setting set to 0 made it return the 2 pages I had set to that template, which could then be excluded. It then output all the other children except for those 2, as I think you wanted.

Credit for solution: Older Stack Overflow answer

发布评论

评论列表(0)

  1. 暂无评论