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?
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 } ?>`
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"
OK. thanks. I will try your suggestions :)
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!
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";
}
}
?>
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
- Products
- Custom Orders
- Design Ideas
- Deliveries
- Promotions + Coupons
- Contact Us
Thanks a lot!
I simply rounded the follwing out:
if (empty($parent->post_content) and $first_child)