Skip to content

Instantly share code, notes, and snippets.

@birgire
Last active October 13, 2019 04:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save birgire/613fab0ceb5d94351c24 to your computer and use it in GitHub Desktop.
Save birgire/613fab0ceb5d94351c24 to your computer and use it in GitHub Desktop.
Change Parent Name with wp_list_pages?
<?php
/**
* Method 1: Use the the_title() filter to modify the first link name in wp_list_pages().
*
* @see http://wordpress.stackexchange.com/questions/156742/change-parent-name-with-wp-list-pages
*/
// -----------------------------------------
// Place this wpse_title() function definition into
// the functions.php file in the current theme directory.
//
function wpse_title( $title )
{
remove_filter( current_filter(), __FUNCTION__ );
return __( 'Overview' );
}
// -----------------------------------------
if ( is_page() ) {
$parent = get_post($post->post_parent);
$parent_title = get_the_title($parent);
$grandparent = $parent->post_parent;
$grandparent_title = get_the_title($grandparent);
$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
// is the homepage the granparent? = third level page
if ($grandparent == is_page('0')) {
// Begin Mod:
add_filter( 'the_title', 'wpse_title' );
$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0"); // list the parent page
// End Mod.
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
if ($children) {
?>
<ul class="submenu">
<?php echo $children; /*print list of pages*/ ?>
</ul>
<?php
}
// is the homepage the parent? = second level page
} elseif ($post->post_parent ==is_page('0')) {
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) {
?>
<ul><li class="current_page_item"><a href="<?php echo get_permalink( $parentid ) ?>">Overview</a></li><?php echo $children; ?></ul>
<?php
} else {// your else stuff
} } }
?>
<?php
/**
* Method 2: Construct a direct link above wp_list_pages().
*
* @see http://wordpress.stackexchange.com/questions/156742/change-parent-name-with-wp-list-pages
*/
if ( is_page() ) {
$parent = get_post($post->post_parent);
$parent_title = get_the_title($parent);
$grandparent = $parent->post_parent;
$grandparent_title = get_the_title($grandparent);
$current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
// is the homepage the granparent? = third level page
if ($grandparent == is_page('0')) {
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0"); // append the list of children pages to the same $children variable
if ($children) {
?>
<ul class="submenu">
<li>
<a href="<?php echo get_permalink( $post->post_parent ); ?>">Overview</a>
</li>
<?php echo $children; /*print list of pages*/ ?>
</ul>
<?php
}
// is the homepage the parent? = second level page
} elseif ($post->post_parent ==is_page('0')) {
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) {
?>
<ul><li class="current_page_item"><a href="<?php echo get_permalink( $parentid ) ?>">Overview</a></li><?php echo $children; ?></ul>
<?php
} else {// your else stuff
} } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment