Forums

list ONLY page siblings? (19 posts)

  1. lorensson
    Member
    Posted 1 year ago #

    I've Googled this for ages now, and the only code I can get ahold of lists siblings WITH a parent heading. I want to ONLY list sibling pages, NOT any parent(s).

    Anyone know of a way to do this using standard template tags?

    For example, if my page structure was:

    1. veggies
      • carrots
      • peas
      • potato
    2. fruits
      • apples
      • pears
      • plums

    if I were on 'Plums', i want to show a list of:

    • apples
    • pears
    • plums

    I do not want any list to show if the user is on 'fruits' or 'veggies'.

  2. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

  3. lorensson
    Member
    Posted 1 year ago #

    Thanks for the reply esmi, I should have been more clear.

    It seems the list_subpages function only lets me list subpages of the current page. It lets me list siblings, but I don't know how to make that list only show up when on a grandchild page of a particular parent page.

    Essentially, I only want this list of siblings to show under a particular parent page which has children and grandchildren. I want the list to only show on the grandchildren pages.

    So for example, if this was my sitemap:

    1. fruits
      1. red fruits
        1. apples
        2. strawberries
        3. cherries
      2. green fruits
        1. kiwis
        2. limes
        3. pears
    2. veggies
      1. yucky veggies
        1. squashes
        2. peas
      2. yummy veggies
        1. carrots
        2. potatoes
    3. favorites
      1. pizza
      2. Del Taco
        1. Macho Combo Burrito

    I want to ONLY show the siblings of the grandchildren of Fruits when on a grandchild of fruits, and only grandchildren of Fruits, not of Veggies or Favorites. So If I'm on Cherries, I want to see links to Cherries, Apples and Strawberries.

    Can anyone help with this? AAt this point, I'm even willing to use a custom page template, but as this is for a client I cannot expect them to manually add page ID's to a PHP file. It must be automated to some degree.

    Thanks in advance for any advice. I hope I've been clear enough.

  4. Chip Bennett
    Member
    Posted 1 year ago #

    You'd need to pass the ID of the parent page to to the call to wp_list_pages, which you can get via:

    global $page
    $parent = $page->page_parent;

    And then you can pass that to wp_list_pages(), e.g.

    wp_list_pages( 'child_of=$parent' );

    (something like that)

  5. lorensson
    Member
    Posted 1 year ago #

    Thanks for your reply Chip.

    That's *just* beyond my PHP abilities I'm afraid. Could I bother you to mark something up for me?

  6. esmi
    Theme Diva & Forum Moderator
    Posted 1 year ago #

    See this section of the Codex page. Or see http://pastebin.com/r3c51F4U

  7. lorensson
    Member
    Posted 1 year ago #

    Thanks esmi. I wish I understood PHP a bit better. I know enough to get through most common WordPress functions, but not much else.

  8. Chip Bennett
    Member
    Posted 1 year ago #

    I'm sorry; I typed way too quickly. You want to use $post rather than $page

    To output what you want, it should be something like this:

    <ul>
    <?php
    global $post
    $current_page_parent = ( $post->post_parent ? $post_post_parent : $post->ID );
    
    wp_list_pages( 'title_li=&child_of=$current_page_parent&depth=1' );
    ?>

    Explanation:

    title_li= - this ensures that the function outputs only the list items, and not the containing UL. More importantly, ensures that the current Page is not output as a header/title for the list.

    (If you want to output such a header, omit the containing

      tags, as well as the title_li= argument.)

      depth=1 - ensures that only sibling pages are included, and not any children of sibling pages.

      (If you want to include children of sibling pages, then omit this argument.)

      child_of=$current_page_parent - returns the ID of the current Page's parent Page, so that the current Page's siblings are output.

    • Chip Bennett
      Member
      Posted 1 year ago #

      Aargh. Fat-fingered the code again. Should be this:

      <ul>
      <?php
      global $post
      $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
      
      wp_list_pages( 'title_li=&child_of=$current_page_parent&depth=1' );
      ?>
    • Chip Bennett
      Member
      Posted 1 year ago #

      Third time's a charm:

      <ul>
      <?php
      global $post
      $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
      
      wp_list_pages( 'title_li=&child_of=$current_page_parent&depth=1' );
      ?>
      </ul>
    • lorensson
      Member
      Posted 1 year ago #

      Hi Chip, I'm getting a syntax error with your last bit of code, the line starting with $current_page_parent

      Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';'

      Any ideas?

    • Chip Bennett
      Member
      Posted 1 year ago #

      Any ideas?

      Yeah: global $post needs a semicolon:

      global $post;
    • lorensson
      Member
      Posted 1 year ago #

      <ul>
      <?php
      global $post
      $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
      
      wp_list_pages( 'title_li=&child_of=$current_page_parent&depth=1' );
      ?>
      </ul>

      This is returning a list of all top-level pages on the site. Any ideas why?

    • lorensson
      Member
      Posted 1 year ago #

      <ul>
      <?php
      global $post;
      $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
      
      wp_list_pages( 'title_li=&child_of=$current_page_parent&depth=1' );
      ?>
      </ul>

      For some reason this comment system is removing semi-colon after global $post

    • Chip Bennett
      Member
      Posted 1 year ago #

      Not entirely sure. If it is a problem with the wp_list_pages() syntax, you could try adding the arguments as an array, instead of a string:

      <ul>
      <?php
      global $post;
      $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
      
      wp_list_pages( array(
           'title_li' => '',
           'child_of' => $current_page_parent,
           'depth' => '1' );
      ?>
      </ul>

      Are you viewing the output from a static Page?

    • Chip Bennett
      Member
      Posted 1 year ago #

      Crap; syntax fail (again). Let me try again:

      <ul>
      <?php
      global $post;
      $current_page_parent = ( $post->post_parent ? $post->post_parent : $post->ID );
      
      wp_list_pages( array(
           'title_li' => '',
           'child_of' => $current_page_parent,
           'depth' => '1' )
      );
      ?>
      </ul>
    • lorensson
      Member
      Posted 1 year ago #

      That seems to have done the trick Chip. Thanks a lot man I owe you a beer!

    • Chip Bennett
      Member
      Posted 1 year ago #

      Glad you got it working! :)

    • SigInTheHead
      Member
      Posted 5 months ago #

      Opened an account just to thank you for this code Chip.

      works a treat.

    Topic Closed

    This topic has been closed to new replies.

    About this Topic