WordPress.org

Ready to get started?Download WordPress

Forums

Hide Child Pages Except When On Parent OR Related Child Pages (8 posts)

  1. Ryan Paul
    Member
    Posted 5 years ago #

    I've found the Flexi Pages widget, and the Collapsing Pages plugin that do most of what I want, but not all.

    Is there a way of showing only top level pages in the menu, and only show child pages when on the parent or related child pages?

    I really don't want to use a plugin, and wanna know if there's a wizard out there that can give me a bit of php to play with?

  2. MichaelH
    Member
    Posted 5 years ago #

    This works in the WordPress Default theme's wp-content/themes/default/sidebar.php file:

    <?php
    $output = wp_list_pages('echo=0&depth=1&title_li=<h2>Top Level Pages </h2>' );
    if (is_page( )) {
      $page = get_query_var('page_id');
      if ($post->post_parent) {
        $page = $post->post_parent;
      }
      $children=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=' );
      if ($children) {
        $output = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=<h2>Child Pages</h2>');
      }
    }
    echo $output;
    ?>
  3. Ryan Paul
    Member
    Posted 5 years ago #

    Hey there...

    This works to a certain extent, but it also opens up the child pages on OTHER parent pages.. even if you're not on them.

    Any ideas how to fix it?

  4. MichaelH
    Member
    Posted 5 years ago #

    Sorry, I'm not seeing that problem.

    When I visit main page, all top level pages are listed in the sidebar.

    When I click on a top level page with no children, all top level pages are listed.

    When I click on a top level page with children, just the children pages are listed.

    When I click on a children page, just the children of that parent are listed.

  5. Ryan Paul
    Member
    Posted 5 years ago #

    When I click on a top level page with children... the child pages are listed for the page I click on... but also ALL the other child pages of other parents :(

  6. MichaelH
    Member
    Posted 5 years ago #

    Disable all plugins, remove Widgets, change to the WordPress Default Theme, put that code in your sidebar.php in place of the wp_list_pages there.

  7. MichaelH
    Member
    Posted 5 years ago #

    Haven't heard back from you so guessing it may work now...

    If not maybe have to global $post in your case

    So after the <?php add global $post;

  8. Ryan Paul
    Member
    Posted 5 years ago #

    Nope it's not working...

    The problem is this... I MUST use the following str_replace lines

    <?php
    global $post;
    $pages = wp_list_pages('echo=0&depth=1&title_li=&include=17&sort_column=menu_order' );
    if (is_page( )) {
      $pages = get_query_var('page_id');
      if ($post->post_parent) {
        $pages = $post->post_parent;
      }
      $child=wp_list_pages( 'echo=0&child_of=' . $page . '&title_li=&include=17&sort_column=menu_order' );
      if ($child) {
        $pages = wp_list_pages ('echo=0&child_of=' . $page . '&title_li=&include=17&sort_column=menu_order');
      }
    }
    $pages = str_replace('The Page Title', '<img src="http://www.domain.ca/wp-content/themes/ctj2b/images/logo.gif" border="0" />', $pages);
    $pages = str_replace('title="<img src="http://www.domain.ca/wp-content/themes/mctj2b/images/logo.gif" border="0" />"', 'title="The Page Title', $pages);
    echo $pages;
    ?>

    I MUST replace a page title text with an image... how can I make it work with these following two replacing statements?

Topic Closed

This topic has been closed to new replies.

About this Topic