WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to hide child pages in submenu? (11 posts)

  1. Hardhat
    Member
    Posted 5 years ago #

    Hi folks

    I'm trying to figure a way of NOT displaying child pages of subpages in a sidebar. This is because the number of child pages getting would make the sidebar excessively long to navigate.

    Ideally I just want to present top-level links (ie: subpages) in the right sidebar for users to click on. The intention is that users will get to see the child pages listed on the next page they click to.

    I hope this makes sense.

    FYI: I'm using an amended version of an Andreas Viklund template which uses the following code:

    // List Subpages - Code from a plugin by Rob Miller (http://robm.me.uk/). Thanks Rob!
    function list_subpages_andreas01($return = 0) {
    global $wpdb, $post;
    $current_page = $post->ID;
    while($current_page) {
    $page_query = $wpdb->get_row("SELECT ID, post_title, post_parent FROM $wpdb->posts WHERE ID = '$current_page'");
    $current_page = $page_query->post_parent; }
    $parent_id = $page_query->ID;
    $parent_title = $page_query->post_title;
    if($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id'")) {
    echo'<div id="subpages"><h2>Subpages for '; echo $parent_title; echo':</h2> <ul class="submenu">';
    $html = wp_list_pages("child_of=$parent_id&depth=$depth&echo=".(!$return)."&title_li=0&sort_column=menu_order");
    echo'</ul></div>'; }
    if($return) {
    return $html;
    } else {
    echo $html; } }
    ?>

    I've tried reading the codex pages on the list_pages tag, but my PHP skills aren't that great and I'm having an attack of real stupidity!

    All help appreciated.

    Hardhat :)

    PS: I'm working offline, on my Mac, using a MAMP installation of WP.

  2. langsuyar
    Member
    Posted 5 years ago #

    This is the parameter you're looking for

    depth (integer)
    This parameter controls how many levels in the hierarchy of pages are to be included in the list generated by wp_list_pages. The default value is 0 (display all pages, including all sub-pages).

    * 0 - Pages and sub-pages displayed in hierarchical (indented) form (Default).
    * -1 - Pages in sub-pages displayed in flat (no indent) form.
    * 1 - Show only top level Pages
    * 2 - Value of 2 (or greater) specifies the depth (or level) to descend in displaying Pages.

    Your code already sets the depth through a variable - you'll have to figure out where $depth is getting defined in your theme and modify it. Alternatively, you can change the current code to read

    $html = wp_list_pages("child_of=$parent_id&depth=1&echo=".(!$return)."&title_li=0&sort_column=menu_order");

    and see what happens.

  3. Hardhat
    Member
    Posted 5 years ago #

    Hi Langsuyar

    The code you supplied worked exactly as I'd hoped, hiding the child pages for subpages...except if hides the child pages when you click on anyone of those subpage links. In other words, it's an 'all or nothing' global rule).

    Any idea how I can make it more granular?

    If not, whereabout is the code would be the best place to look for, or define, the $depth for a particular template?

    Best wishes

    H :)

  4. langsuyar
    Member
    Posted 5 years ago #

    That's hard to remote-debug, your theme is doing some unconventional stuff there. Looks like you might be changing a global function that is used several times in the theme. You might want to keep that code the way it was originally, and look at the sidebar code in particular. I got a feeling that the theme passes in the $depth variable from wherever pages are listed. Find the sidebar .php code and see what's happening there. If you have a decent editor, you can do a Find In Files and search for $depth to find all occurrences.

  5. Hardhat
    Member
    Posted 5 years ago #

    Thanks for the tips L.

    I'll give it a go and report back.

    Best wishes.

    H :)

  6. Hardhat
    Member
    Posted 5 years ago #

    OK. I've looked into things a bit further and think I realise what the real problem is.

    As far as I can work out the theme is doing exactly what it should do in terms of defining the $depth for child pages. All of the various functions within the theme (ie: list subpages, etc) are contained within a functions.php file.

    The problem I face is that I think I may not have defined the problem accurately.

    My real problem is to not show secondary child pages on only certain top level parent pages.

    I suppose the question I'm asking is: is it possible to write a one-off rule (be php or CSS) that I can incorporate into certain template pages to not show the secondary children pages? The idea is that, by using a separate template page, the secondary children would then be shown on any first-level child page that is viewed.

    Hope that makes sense! lol

    Again, any input appreciated.

    H :)

  7. Hardhat
    Member
    Posted 5 years ago #

    And, in a moment of low wattage intelligence, I answered my own question!

    The easiest solution is to use a CSS rule to hide the offending child pages.

    To illustrate: in my original css the rule that contained the styling for the second-child links read:

    #subpages ul.submenu li ul li a{ color: #7d6f61; background-color: #f4f2e2; text-transform: lowercase; text-align: right; margin-left: -1em; padding: 3px 5px 3px 3px; border-right: 4px solid #f90; border-left-width: 0; }

    By changing the display property to none, the offending links vanished. The final rule simply reads:

    #subpages ul.submenu li ul li a{ display: none; }

    Voila!

    File under 'hiding in plain sight'!

    Many thx to Langsuyar for the earlier help.

    H :)

  8. depperson
    Member
    Posted 5 years ago #

    HardHat:

    Maybe you can give me insight, since you seem to have figured out your situation.

    I am using the following code to display sub-pages (we'll call 1st level down) from top level link:

    <?php
      if($post->post_parent)
      $children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
      else
      $children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
      if ($children) { ?>
      <ul>
      <?php echo $children; ?>
      </ul>
      <?php } ?>

    this is taken directly from the WP codex. And it works great, except when I click to a 2nd level sub-page (subpage of a subpage). At that point, all the 2nd level sub-pages show up in the menu.

    I've tried using depth= syntax, but doesn't work. What would I look for in my CSS to see if that can be hardcoded to remove the 2nd level sub-page menu?

  9. jonstarbuck
    Member
    Posted 5 years ago #

    Hi,

    I have found $depth defined in:

    /wp-includes/atomlib.php

    and I changed line 73 to:

    var $depth = 2;

    but it has made no difference... what did i do wrong?

    Thanks, Jon.

  10. knds
    Member
    Posted 5 years ago #

    Here is an example:
    <?php wp_list_pages('title_li=&depth=1'); ?>

    you need to add/change the "depth" parameter/argument within the function and not inside any file.

    hope this helps :)

    knds

  11. mbarrio
    Member
    Posted 5 years ago #

    Hey langsuyar thanks for the post earlier

    $html = wp_list_pages("child_of=$parent_id&depth=1&echo=".(!$return)."&title_li=0&sort_column=menu_order");

    and see what happens.

    Worked perfect for my function

    function themefunction_list_pages() {
    add_filter('wp_list_pages','themefunction_alter_list_pages');
    wp_list_pages("child_of=$parent_id&depth=1&echo=".(!$return)."&title_li=0&sort_column=menu_order");
    remove_filter('wp_list_pages','themefunction_alter_list_pages');

Topic Closed

This topic has been closed to new replies.

About this Topic