views:

1156

answers:

2

not sure if this is programming enough or if it should go to superuser, which is not programming AT ALL. I leave it to the mod to decide.

I created a menu using get_pages() and specifying the parent id. This parent id has children and sub-children and sub-subchildren. I need each of these (sub)children to have the parent id displayed, as this menu sits in the header.php file. So it will be included for all pages no matter their ancestry, and i would like that their section main item receives a specific class "currentlyActive".

The code i made ony works for children, not for sub/ sub-sub children

<li id="infographieButton"  <?php echo ($post->ID == 5  || $post->post_parent == 5)? 'class="currentlyActive"': ''; ?>>
                                <a href="<?php bloginfo('url')  ?>/infographie/" class="menuHeader"><span>Infographie</span></a>
                                <ul id="dropdownmenuInfographie"  class="submenu">
                                    <?php
                                    $pages = get_pages('child_of=5&parent=5&sort_column=menu_order&sort_order=asc&title_li=');
                                    foreach($pages as $page) {
                                        ?>
                                    <li <?php echo ($post->ID == $page->ID)? 'class="current_page_item"': ''; ?>><a href="<?php echo get_page_link($page->ID) ?>"><?php
                                                echo $page->post_title;
                                                ?></a></li>

                                    <?php
                                    }
                                    ?>
                                </ul>
                            </li>

I'm a bit puzzled on how to achieve that in wordrpress. Any suggestion is welcomed!

A: 

Here's what the Wordpress documentation says (my emphasis):

child_of

Displays the sub-pages of a single Page only; uses the ID for a Page as the value. Defaults to 0 (displays all Pages). Note that the child_of parameter will also fetch "grandchildren" of the given ID, not just direct descendants.

parent

Displays those pages that have this ID as a parent. Defaults to -1 (displays all Pages regardless of parent). Note that this can be used to limit the 'depth' of the child_of parameter, so only one generation of descendants might be retrieved. You must use this in conjuction with the child_of parameter. Feed it the same ID.

Given that, all you need to do is remove the parent argument—assuming what you want is a flat list of descendants and not a hierarchical one. To display the parent ID of any given page in your list, I reckon you just need to access a specific property of that page object (probably named something like parent_id).

Jakob
yes, but i need the parent_id of the parent, and the parent_id of the parent_id of the parent. See what i'm into ?
pixeline
In that case, I don't understand what you're after. Your current code doesn't show any need for parent IDs. It would be helpful if you could show an example of the type of PHP array you want to get back.
Jakob
i realize the code i posted was not enough to understand my problem. I've just rephrased and posted a more comprehensive code. Thank you for hanging around.
pixeline
I don't have time to help you with a complete solution, but I can tell you that recursion would be a good bet (as it usually is when creating or traversing tree structures).
Jakob
Well, if you haven't used recursion before, you'd have to look up some tutorials on the topic. Example use of recursion when traversing a file system should be helpful, because it's basically the same scenario. Then again, if the number of hierarchy levels is known, the problem could also be solved by nested loops. If the number is completely arbitrary, however, the most practical solution is recursion. Try a Google search for "PHP recursion," and then try to refine it to find examples of file system traversal.
Jakob
+1  A: 

See you've asked the same question at http://wordpress.org/support/topic/321046

See http://codex.wordpress.org/Function%5FReference/get%5Fpost%5Fancestors

Michael
Is posting on different sites a problem? I think it makes sense.Anyway, your suggestion is excellent, never found that function before.Since then, i googled for it and found someone with exactly the same problem: http://photogabble.co.uk/2009/04/17/wordpress-get_post_ancestors/Thank you Michael!
pixeline