UPDATE: I uploaded a newer version of the function after a few tweaks that I made recently. Same functionality, just fixing issues.
When you have to customize a WordPress theme typically as a developer you are left with the options that the default WordPress functions give you. As I have found after customizing multiple themes to fit a users website design being able to create some different output can help significantly. Now this can take multiple functions but if you start to save them they can come it handy.
This is meant to be a replacement for the wp_list_pages() function that is used in WordPress currently. The documentation for this can be found at: http://codex.wordpress.org/Template_Tags/wp_list_pages
The function that I have just developed should help to customize page output of the navigation for any theme. I personally have used this to create custom output for the top level navigation and bottom level navigation. I have included the documentation in the function declaration. Please feel free to copy and use this as necessary. I would greatly appreciate it if you could leave my information in the comments.
<?php
function wp_list_pages_custom($args, $custom_args, $debug = false) {
//Usage
/*
This is meant to be a replacement function for wp_list_pages which can be found here:
http://codex.wordpress.org/Template_Tags/wp_list_pages
It takes in the same arguments as that function plus some other custom arguments to help
change the output.
$args
--------------------------
This is the same as the wp_list_pages function, a link can be found above
$custom_args
--------------------------
This is where the user can customize the way the output is displayed, options can be found
below.
$debug
--------------------------
This is set to false by default, if you would like to change it to true you will see output
at a few places. This is mainly for testing purposes if you need to change something.
The custom arguments have the following default options:
"",
'tag_close' => "",
'use_link' => 0,
'use_class' => 0 ); ?>
Options:
tag_open
---------------------------
This can take any opening string you wish to use. This could be something like "<p>" or "<div class='page_item'>".
tag_close
---------------------------
This can take any opening string you wish to use. This could be something like "</p>" or "</div>".
use_link
---------------------------
This takes a boolean value in numeric format. 0 = false, 1 = true.
use_class
---------------------------
This takes a boolean value in numeric format. 0 = false, 1 = true.
Example usage:
<?php
echo wp_list_pages_custom("sort_column=menu_order&echo=0&title_li=", "tag_open=<p>&tag_close=</p>&use_link=1&use_class=1");
?>
*/
//Default variables
$tag_open = "";
$tag_close = "";
$use_link = 0;
$use_class = 0;
$custom_pages = "";
$default_echo = 0;
$default_pages = "";
$get_args = array();
//Get custom arguments
parse_str($custom_args, $get_args);
$tag_open = (array_key_exists('tag_open',$get_args)?$get_args['tag_open']:"");
$tag_close = (array_key_exists('tag_close',$get_args)?$get_args['tag_close']:"");
$use_link = (array_key_exists('use_link',$get_args)?$get_args['use_link']:"");
$use_class = (array_key_exists('use_class',$get_args)?$get_args['use_class']:"");
//By default dont echo, just return value
$default_echo = 0;
if(preg_match("/^(.*?)(echo=([0-9]))(.*?)$/msi", $args, $match)) {
$default_echo = $match[3];
}else{
$default_echo = "1";
}
if($debug == true){
echo "Default Echo Argument: $default_echo<br />";
}
//Replace the echo so we can alter it
if(preg_match("/^(.*?)(echo=[0-9])(.*?)$/msi", $args)) {
$new_args = preg_replace("/^(.*?)(echo=[0-9])(.*?)$/msi", "$1"."echo=0"."$3", $args);
}else{
$new_args = $args."&echo=0";
}
if($debug == true){
echo "Arguments Passed: $new_args<br />";
}
$default_pages = wp_list_pages($new_args);
if($debug == true){
echo "Before Alteration Output: $default_pages<br />";
}
//Change the output to use specified tags
if(preg_match_all("/^(<li>)<a>(.*?)</a>(</li>)$/msi", $default_pages, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
//Choose whether to use the default classes, it will insert into the tag
if($use_class == 1) {
$temp_tag = preg_replace("/>/msi"," class="".$match[2]."">", $tag_open);
$custom_pages .= $temp_tag;
}else{
$custom_pages .= $tag_open;
}
//Check how to display the text (with or without link)
if($use_link == 1){
$custom_pages .= "<a>".$match[4]."</a>";
}else{
$custom_pages .= $match[4];
}
$custom_pages .= $tag_close;
}
}
if($debug == true){
echo "After Alteration Output: $custom_pages<br />";
}
//Output
if($default_echo == 1)
echo $custom_pages;
else
return $custom_pages;
}
?>
To implement this function all that a user must do is copy the function below and paste it into the bottom of the functions.php file found in the theme directory that the current WordPress installation is using. This is a generalized installation method so be careful to backup that file before changing it. If it is not an extremely customized theme the function should be able to be pasted at the bottom of the functions.php page and used thereafter.
As a side note, this has not been developed to deal with the “depth” option for the wp_list_pages() function. As it is it will most likely replace all the <li> and </li> but leave the surrounding <ul> and </ul>.