• lorensson

    (@lorensson)


    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
    • 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’.

Viewing 15 replies - 1 through 15 (of 18 total)
  • esmi

    (@esmi)

    Thread Starter lorensson

    (@lorensson)

    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
    • green fruits
    1. kiwis
    2. limes
    3. pears
    • veggies
    1. yucky veggies
    1. squashes
    2. peas
    • yummy veggies
    1. carrots
    2. potatoes
    • 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.

    Chip Bennett

    (@chipbennett)

    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)

    Thread Starter lorensson

    (@lorensson)

    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?

    esmi

    (@esmi)

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

    Thread Starter lorensson

    (@lorensson)

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

    Chip Bennett

    (@chipbennett)

    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

      (@chipbennett)

      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

      (@chipbennett)

      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>

      Thread Starter lorensson

      (@lorensson)

      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?

      Any ideas?

      Yeah: global $post needs a semicolon:

      global $post;

      Thread Starter lorensson

      (@lorensson)

      <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?

      Thread Starter lorensson

      (@lorensson)

      <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

      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?

      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>

    Viewing 15 replies - 1 through 15 (of 18 total)
    • The topic ‘list ONLY page siblings?’ is closed to new replies.