1

I use the following line in a code which outputs subpages and a parent. This code outputs the name of the parent page. I would like to replace this with the word "Overview."

How do I achieve this in wp_list_pages with as little code as possible? Thanks!

$children = wp_list_pages("title_li=yo&include=".$post->post_parent."&echo=0"); // list the parent page

The full code:

<?php
    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=&include=".$post->post_parent."&echo=0"); // list the parent page
        $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
        } } }
?>
2

2 Answers 2

1
+50

You could of course use some preg_replace() tricks to solve this, but here's a little (untested) idea using the the_title filter instead:

add_filter( 'the_title', 'wpse_title' ); 
$children = wp_list_pages("title_li=&include=".$post->post_parent."&echo=0");    

where our filter callback is:

function wpse_title( $title )
{
    remove_filter( current_filter(), __FUNCTION__ );
    return __( 'Overview' );
}

Remark 1:

I think you should consider replacing the wp_list_pages() method of generating the single parent link and instead use get_permalink( $parent_id ) or get_page_link( $parent_id ) from the parent page ID. Then you can easily from control the link name.

 <li>
      <a href="<?php echo get_permalink( $parent_id ); ?>">Overview</a>
 </li>

You already use this for one of the levels, so why not for others as well?

Remark 2:

You could also use get_post_ancestors() to help you get the current tree level position.

For example:

$ancestors_ids = get_post_ancestors( get_the_ID() );
$level         = count( $ancestors_ids ) ;

Then you can get the parent ID, of the current page, with:

$parent_id = ( $level > 0 ) ? array_shift( $ancestors_ids ) : 0;

Here's an example of how the case $level = 2 could look like:

<ul class="submenu">
    <li>
        <a href="<?php echo get_permalink( $parent_id ); ?>">Overview</a>
    </li>
    <?php echo wp_list_pages("title_li=&child_of=".$parent_id."&echo=0&depth=1"); ?>
</ul>

Maybe your $level = 1 could then be:

<ul class="submenu">
    <li>
        <a href="<?php echo get_permalink( $parent_id ); ?>">Overview</a>
    </li>
    <?php echo wp_list_pages("title_li=&child_of=".get_the_ID()."&echo=0&depth=1"); ?>
</ul>

etc ..

4
  • Remark #1 makes sense to me, but I'm not sure what that would look like in the completed code. Any help? Commented Aug 9, 2014 at 19:24
  • ...any help...? Commented Aug 10, 2014 at 10:06
  • Hi @AndrettiMilas. Did you try the filter method? Here's one idea regarding remark #1, but might need adjustments. I think it would be informative if you could provide the wanted HTML structure for the submenu on each level, to clear up any possible misunderstanding about it.
    – birgire
    Commented Aug 10, 2014 at 10:15
  • I have very little PHP knowledge and I don't know how to incorporate it into the code, could you be as kind as to post the full code? themeforward.com/demo2/practice-areas/criminal-law Commented Aug 10, 2014 at 18:22
0

Try this code snippets once:

$args = array('post_type' => 'page', 'parent' => 0);
  $parent_pages = get_pages($args);
  if( $parent_pages ){
    echo '<ul>' . "\n";
    foreach( $parent_pages as $page){
      $children = wp_list_pages("title_li=&child_of=".$page->ID."&echo=0");
      if( $children ) {
        echo '<li><a href="'. get_page_link( $page->ID ) .'">Overview</a></li>' . "\n"  ;
        echo '<ul>' . "\n";
        echo $children;
        echo '</ul>' . "\n";
      }else{
        echo '<li><a href="'. get_page_link( $page->ID ) .'">' . $page->post_title . '</a></li>' . "\n" ;
      }
    }
    echo '</ul>' . "\n";
  }

But I have a question. what will happen with child pages of sub pages? If one sub page treat as a parent page then it will convert to overview text?

3
  • I need a way to do it contained in that code. The code I included is from a larger code. Commented Aug 4, 2014 at 18:54
  • The concept is that first you will grab all parent pages. then you will retrieve the child pages best on parent page and if there have child pages then you'll convert the parent title with "Overview" text. My code is following this. if it will help you then you'll re-organize my code with your existing code & CSS. Commented Aug 4, 2014 at 19:20
  • This doesn't help me, sorry. Commented Aug 4, 2014 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.