开发者

Wordpress : template tags get_page()

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 childr开发者_如何学运维en, 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!


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

See http://codex.wordpress.org/Function_Reference/get_post_ancestors


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜