好的,这段代码应该会让您走上正确的道路。它的设计目的并不是要完全满足您的需要,但从上面的代码来看,您似乎相当符合要求,因此应该能够根据需要进行修改。我还建议您看看Walker_Page
类,这样您就可以将下面的方法与Walker_Page
, 并查看已更改的内容(wp includes/post-template.php)。
将此放入functions.php
, 然后打电话的时候wp_list_pages($args)
, 确保包含参数\'walker\' => new Walker_Custom_Page
.
<?
/**
* Custom Walker for creating the page list.
*/
class Walker_Custom_Page extends Walker_Page {
/**
* @see Walker::start_lvl()
*
* @param string $output Passed by reference. Used to append additional content.
* @param int $depth Depth of page. Used for padding.
*/
function start_lvl(&$output, $depth) {
$indent = str_repeat("\\t", $depth);
$output .= "\\n{$indent}<ul class=\'children\'>\\n";
}
/**
* @see Walker::start_el()
*
* @param string $output Passed by reference. Used to append additional content
* @param object $page Page data object
* @param int $depth Depth of page. Used for padding
* @param array $args
* @param int $current_page Page ID
*/
function start_el(&$output, $page, $depth, $args, $current_page) {
/** Add an indent (if required) before the page */
if($depth) :
$indent = str_repeat("\\t", $depth);
else :
$indent = \'\';
endif;
/** Make \'new_href\' and \'image\' arrays to avoid errors if it is empty */
if(!is_array($args[\'new_href\'])) : $args[\'new_href\'] = array($args[\'new_href\']); endif;
/** Check to see if an alternitive link has been specified and set the $link_href **/
if(array_key_exists((string)$page->ID, $args[\'new_href\'])) :
$link_href = $args[\'new_href\'][(string)$page->ID];
$target = \'\';
elseif(array_key_exists(\'_\'.(string)$page->ID, $args[\'new_href\'])) :
$link_href = $args[\'new_href\'][\'_\'.(string)$page->ID];
$target = \' target="_blank"\';
else :
$link_href = get_permalink($page->ID);
$target = \'\';
endif;
/** Make the link for the page */
$page_children = get_pages(\'child_of=\'.$page->ID);
$link_in = (in_array($page->ID, $args[\'no_link\'])) ? \'<a>\' : \'<a href="\'.$link_href.\'"\'.$target.\'>\';
$link_out = \'</a>\';
$link_after = (count($page_children) !== 0 && $depth !== 0) ? \' »\' : false;
$link = $link_in.$link_before.apply_filters(\'the_title\', $page->post_title, $page->ID).$link_after.$link_out;
/** Extract the args */
extract($args, EXTR_SKIP);
/** Make the CSS class for the link */
$css_class = array(\'page_item\', \'page-item-\'.$page->ID);
$current_page = apply_filters(\'change_current_page\', $current_page);
if(!empty($current_page)) :
$_current_page = get_page($current_page);
_get_post_ancestors($_current_page);
if(isset($_current_page->ancestors) && in_array($page->ID, (array) $_current_page->ancestors)) :
$css_class[] = \'current_page_ancestor\';
endif;
if($page->ID == $current_page) :
$css_class[] = \'current_page_item\';
elseif($_current_page && $page->ID == $_current_page->post_parent) :
$css_class[] = \'current_page_parent\';
endif;
elseif($page->ID == get_option(\'page_for_posts\')) :
$css_class[] = \'current_page_parent\';
endif;
/** Filter the page CSS class so that extra can be added */
$css_id = $css_class[1];
$css_class = implode(\' \', apply_filters( \'page_css_class\', $css_class, $page, $depth, $args, $current_page));
/** Add the page link to $output */
$output .= $indent . \'<li id="\'.$css_id.\'" class="\' . $css_class . \'">\' . $link;
/** Add the time that the page was modified (if desired) */
if(!empty($show_date)) :
if(\'modified\' == $show_date) :
$time = $page->post_modified;
else :
$time = $page->post_date;
endif;
$output .= \' \'.mysql2date($date_format, $time);
endif;
}
}
?>