Created
October 28, 2014 13:30
-
-
Save DanBeckett/9b1b9d6aa0896221e56b to your computer and use it in GitHub Desktop.
List WordPress pages in and not in a specific category
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- Quick solution for a question on StackExchange, | |
running through site pages, and compiling a ul of | |
pages in a category and a ul of pages that aren't. --> | |
<?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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment