0

I try to add link to parent page to a working list of childpages. The parent page must be the first item's list whenever the list appear.

Here is the actual code found here :

//* List child pages [jla_childpages]
function jla_list_child_pages() {
  global $post;
    if ( is_page() && $post->post_parent )
      $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->post_parent . '&echo=0' );
    else
      $childpages = wp_list_pages( 'sort_column=menu_order&title_li=&child_of=' . $post->ID . '&echo=0' );
    if ( $childpages ) {
      $string = '<ul id="childpages-menu">' . $childpages . '</ul>';
    }
  return $string;
}
add_shortcode('jla_childpages', 'jla_list_child_pages');

Anyone has an idea how I can transform this to add parent page to be the list first item whenever the list is shown ? Thanks !

1 Answer 1

0

you would add something like

//* List child pages [jla_childpages]
function jla_list_child_pages() {
  global $post;

    $parentID = ( is_page() && $post->post_parent ) ? $post->post_parent : $post->ID;
    $parentPost = get_post($parentID);
    $isParentCurrent = (get_the_ID() == $parentID) ? " current_page_item" : '';

    $parent = "<li class='page_item page-item-{$parentID}{$isParentCurrent}'><a href='".get_permalink( $parentID )."'>{$parentPost->post_title}</a></li>";

    $childpages = wp_list_pages( "sort_column=menu_order&title_li=&child_of={$parentID}&echo=0" );

    if ( $childpages )
      return '<ul id="childpages-menu">' . $parent . $childpages . '</ul>';

}
add_shortcode('jla_childpages', 'jla_list_child_pages');

I haven't tested it, but all you're doing is figuring out the parent before everything, and including the parent above the children in the return.

0

Your Answer

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

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