WordPress:模板标签 get_page()
不确定这是否足够编程,或者是否应该交给超级用户,而超级用户根本没有编程。我把它留给模组来决定。
我使用 get_pages() 创建了一个菜单并指定了父 ID。该父 ID 具有子项、子项和子子项。我需要每个(子)子项都显示父 ID,因为此菜单位于 header.php 文件中。因此,无论其祖先如何,它都将包含在所有页面中,并且我希望它们的部分主要项目接收特定的类“currentlyActive”。
我编写的代码只适用于儿童,不适用于子/子子儿童,
<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>
我对如何在 wordrpress 中实现这一点感到有点困惑。欢迎任何建议!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看到您在 http://wordpress.org/support/topic/321046
请参阅http://codex.wordpress.org/Function_Reference/get_post_ancestors
See you've asked the same question at http://wordpress.org/support/topic/321046
See http://codex.wordpress.org/Function_Reference/get_post_ancestors
这是 WordPress 文档的内容(我的重点):
鉴于此,您需要做的就是删除
parent
参数 - 假设您想要的是一个简单的后代列表,而不是分层列表。要显示列表中任何给定页面的父 ID,我认为您只需要访问该页面对象的特定属性(可能命名为parent_id
)。Here's what the Wordpress documentation says (my emphasis):
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 likeparent_id
).