Forums

How to link directly to first subpage of a parent page (7 posts)

  1. Jack Tummers
    Member
    Posted 3 years ago #

    I have a page menu with a structure like this:
    Animals
    - Cats
    - Dogs
    Cars
    - Opel
    - Ford

    In my template I thus have a main menu like this:
    Animals
    Cars

    When I click on Animals it should lead directly to the first subpage, Cats and not to the main page Animals, because the Animals page itself is empty, only a placeholder so I can create a submenu in another part of the page.

    How can I do that without hardcoding?

  2. Cathy Tibbles
    Member
    Posted 3 years ago #

    You can search for a custom pages widget in the extend>>plugins screen. Or you can enter this inside the loop in the page.php template.
    `<?php
    $children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
    if ($children) { ?>

      <?php echo $children; ?>

    <?php } ?>`
    This will display your subpages when the parent page is clicked on. Unfortunately there will be a 2 level menu. Another click is required to pick the first page. If you want pages in a particular order- use posts, and use the plugin - "In Series"

  3. Jack Tummers
    Member
    Posted 3 years ago #

    OK. thanks. I will try your suggestions :)

  4. hch
    Member
    Posted 3 years ago #

    I am also searching for a solution to the problem described above (like: "if parent page is empty show next/first child of it"). Btw - this is not about 'wp_list_pages'.

    How can I do that with(out) hardcoding?

    Thanks in advance!

  5. actionscripted
    Member
    Posted 3 years ago #

    Here's some rough code that'll build a top-level list that links parent categories to their first child. Use this as a basis for things, and you can step-through children to build a nested list if you'd like by removing the "number=1" bit:

    <?php
              $parents = get_pages('&sort_column=menu_order,post_title&child_of=0&parent=0');
              $current = $post;
    
              while($current->post_parent) {
                $current = get_post($current->post_parent);
              }
    
              foreach ($parents as $parent_index => $parent) {
                if ($parent->post_title == 'Home') continue;
    
                $first_child = get_pages('&sort_column=menu_order,post_title&number=1&child_of=' . $parent->ID . '&parent=' . $parent->ID);
                $parent_link = get_page_link($parent->ID);
    
                if ($first_child) {
                  $child       = $first_child[0];
                  $parent_link = get_page_link($child->ID);
                }
    
                if ($current->ID == $parent->ID) {
                  echo '<li class="current"><a href="' . $parent_link . '">' . $parent->post_title . '</a></li>' . "\r\n";
                } else {
                  echo '<li><a href="' . $parent_link . '">' . $parent->post_title . '</a></li>' . "\r\n";
                }
              }
            ?>
  6. actionscripted
    Member
    Posted 3 years ago #

    And the sample output from the code above:

    <ul>
      <li><a href="http://www.somesite.com/about-us/history/">About Us</a></li>
      <li class="current"><a href="http://www.somesite.com/products/by-category/">Products</a></li>
      <li><a href="http://www.somesite.com/custom-orders/">Custom Orders</a></li>
      <li><a href="http://www.somesite.com/design-ideas/">Design Ideas</a></li>
      <li><a href="http://www.somesite.com/deliveries/">Deliveries</a></li>
      <li><a href="http://www.somesite.com/promotions-coupons/">Promotions + Coupons</a></li>
      <li><a href="http://www.somesite.com/contact-us/">Contact Us</a></li>
    </ul>

    Assuming you have the following page setup:

    • About Us
      • History
    • Products
      • By Category
    • Custom Orders
    • Design Ideas
    • Deliveries
    • Promotions + Coupons
    • Contact Us
  7. hch
    Member
    Posted 3 years ago #

    Thanks a lot!

    I simply rounded the follwing out:
    if (empty($parent->post_content) and $first_child)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.