Last active
August 29, 2015 13:56
-
-
Save michaeldozark/8867579 to your computer and use it in GitHub Desktop.
Wrapper function for wp_list_pages() that accepts more than one ID in exclude_tree and adds an include_tree parameter
This file contains 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
<?php | |
/** | |
* slimline_wp_list_pages function | |
* | |
* Builds include and exclude parameters from a tree before calling | |
* a standard wp_list_pages call. All parameters are the same as the | |
* wp_list_pages functions with one exception listed below. | |
* | |
* @param string $args[ 'include_tree' ] Define a comma-separated list of Parent IDs to be included | |
*/ | |
function slimline_wp_list_pages( $args ) { | |
/** | |
* Set default includes and excludes since these are the only parameters | |
* we are editing with this function. Set echo since it will be used at the | |
* end of the function | |
*/ | |
$includes_excludes = array( | |
'echo' => true, | |
'exclude' => '', | |
'exclude_tree' => '', | |
'include' => '', | |
'include_tree' => '', | |
); | |
// parse and merge args with defaults | |
$args = wp_parse_args( $args, $includes_excludes ); | |
/** | |
* Get page IDs and children from include and exclude trees and append them to the standard include | |
* and exclude arguments | |
*/ | |
$args[ 'exclude' ] = slimline_wp_list_pages_parse_tree( $args[ 'exclude'], $args[ 'exclude_tree' ] ); | |
$args[ 'include' ] = slimline_wp_list_pages_parse_tree( $args[ 'include'], $args[ 'include_tree' ] ); | |
/** | |
* Unset the include and exclude trees (we are done with them) | |
*/ | |
unset( $args[ 'exclude_tree' ] ); | |
unset( $args[ 'include_tree' ] ); | |
/** | |
* Call wp_list_pages or return the value of wp_list_pages depending on the echo parameter | |
*/ | |
if ( $args[ 'echo' ] ) | |
wp_list_pages( $args ); | |
else | |
return wp_list_pages( $args ); | |
} | |
/** | |
* slimline_wp_list_pages_parse_tree function | |
* | |
* Helper function for slimline_wp_list_pages. Generates a comma-separated list for the include or exclude | |
* parameters based on the given value of that parameter and the value of the related tree. | |
* | |
* @param string $list The comma-separated list of includes or excludes | |
* @param string $tree The comma-separated list of include_tree or exclude_tree parent IDs | |
* @return string $list The final comma-separated list of includes or excludes | |
*/ | |
function slimline_wp_list_pages_parse_tree( $list = '', $tree = '' ) { | |
if ( ! $tree ) | |
return $list; // return default or given list value if no tree to parse | |
$list = explode( ',', $list ); // turn $list into an array so we can easily add to it later | |
$tree_array = $tree = explode( ',', $tree ); // turn $tree into an array so we can iterate through it | |
/** | |
* Iterate through the $tree array to get the children of each page in the $tree. Add the IDs of each | |
* child into $tree_array to merge into the $list | |
*/ | |
foreach ( $tree as $parent ) { | |
if ( ! ( $child_pages = get_pages( array( 'child_of' => $parent ) ) ) ) | |
continue; // skip processing if no child pages found for the current parent | |
foreach ( $child_pages as $child_page ) | |
$tree_array[] = $child_page->ID; | |
} | |
$list = array_merge( $list, $tree_array ); | |
return implode( ',', $list ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Originally posted at http://www.michaeldozark.com/tips/even-better-includes-and-excludes-for-wp_list_pages/