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

categories - Using wp_list_pages to create 2 lists of pages and include and exclude some of them depending on their category

programmeradmin2浏览0评论

I would like to create 2 lists of pages and include and exclude some of them depending on their category. In the following example, I have my 2 lists. A list of promoted pages and a list for the rest.

    <ul class="promoted">
        <?php wp_list_pages('include=4');?>
    </ul>
    <ul>
        <?php wp_list_pages('exclude=4');?>
    </ul>

I have added my pages to the specific category (4) but no luck.

I would like to create 2 lists of pages and include and exclude some of them depending on their category. In the following example, I have my 2 lists. A list of promoted pages and a list for the rest.

    <ul class="promoted">
        <?php wp_list_pages('include=4');?>
    </ul>
    <ul>
        <?php wp_list_pages('exclude=4');?>
    </ul>

I have added my pages to the specific category (4) but no luck.

Share Improve this question asked Oct 20, 2014 at 0:47 GabGab 2533 silver badges18 bronze badges 5
  • Please read Codex (or even better: The phpDocBlock in source) again: "Like exclude, this parameter takes a comma-separated list of Page IDs.". That's not the category ID there... – kaiser Commented Oct 20, 2014 at 1:07
  • @kaiser, thanks but yes after testing I can see it won't work and am now asking for help on how to achieve this. The question remains. – Gab Commented Oct 20, 2014 at 1:11
  • Gab, just try something, edit your question with what you know now and after some try and fail. The more you try, the better your question, the more effort you invest, the better the answer. – kaiser Commented Oct 20, 2014 at 1:15
  • @kaiser I get it but i think it's quite clear. I have a category ID I would like to include or exclude from that list if possible. If not, I'll just explicitly use post IDs. – Gab Commented Oct 20, 2014 at 1:30
  • Few remarks: Pages don't support categories by default, so it's not clear what taxonomy you mean here. How are you going to cut the tree in general? For example what if a leaf is in the include category but the rest of the branch isn't? – birgire Commented Oct 28, 2014 at 9:43
Add a comment  | 

2 Answers 2

Reset to default 0 +50

One quick way of doing it without having to set up multiple loops would be to use the get_posts() function and grab all pages, then check through them with in_category() and split out pages that are in the category and pages that are not.

You can then run through the 2 groups of pages separately in your ULs with a simple foreach loop.

<?php $args = array(
    'post_type' =>  'page',
    'post_status'   =>  'publish',
    'numberposts'   =>  -1
);

//Pull pages into an array
$all_pages = get_posts($args);

//Create empty arrays to populate with filtered pages
$in_category = array();
$ex_category = array();

//loop through the page array
foreach($all_pages as $post) {
    //Set data from the current page as globals,
    //to enable WordPress function tags
    setup_postdata($post);

    //Build the format of the link to be returned
    $link_format = '<li><a href="'.get_the_permalink().'">'.get_the_title().'</a></li>';        

    //Check whether the page is in the category and
    //send this link to the relevant array
    if(in_category(4)) {
        $in_category[] = $link_format;
    } else {
        $ex_category[] = $link_format;
    }   
};

//Restore postdata to before we set it to the current post
wp_reset_postdata(); ?>

<ul class="links_in_category_4">
    <?php foreach($in_category as $cat_link) echo $cat_link; ?>
</ul>

<ul class="links_not_in_category_4">
    <?php foreach($ex_category as $cat_link) echo $cat_link; ?>
</ul>

I actually have a large page based website that I am working on. Since it is mainly page driven, I used a function to allow categories and tags on pages. I would love to know if there is a way to exclude a category using the wp_list_pages() function. This would allow me to put in one id or slug rather than updating it with a new page id each time I have a new page that I want to exclude.

To be even more specific, I am using the following function which uses the wp_list_pages() function within it.

function wpb_list_child_pages() { 

global $post; 

if ( is_page() && $post->post_parent )

    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
else
    $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );

if ( $childpages ) {

    $string = '<ul>' . $childpages . '</ul>';
}

return $string;

}

add_shortcode('wpb_childpages', 'wpb_list_child_pages');

That function can be found here: https://www.wpbeginner/wp-tutorials/how-to-display-a-list-of-child-pages-for-a-parent-page-in-wordpress/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论