视差 WordPress 主题。我如何动态获取页面 ID 以显示其自己的子菜单?
我正在创建一个具有视差功能和 html5 的 WordPress 主题,主页具有所有主页和两种不同类型的导航。
主要的一个是父页面的导航。所以我有关于、项目和联系方式。
但每一页都有子页面。关于页面下面有一个子页面,但由于我无法从主菜单访问,因此具有子页面或子页面的每个页面都有第二种类型的菜单。
还有项目,有不同的页面:“项目”(主)、“设计”(子)、咨询(子)等。
第二个菜单作为圆形按钮放置在页面右侧,列出了父级“关于”和孩子“大约2岁”。
我已经想出了一种方法来列出这些内容并使链接的行为与滚动到(视差行为)
但是,我对父页面进行硬编码的方式。我想做的是找到一种动态获取页面 ID 的方法,这样它就不会被硬编码,但由于主题仅作为一个页面网站工作,这让我遇到了麻烦。
这是我的代码,任何人都可以帮我找到一种方法,以便代码能够识别每个当前页面的 ID 以列出主页面和子页面吗?
如您所见, $parent = 13;
是硬编码的,并且它还包含 $pages = get_pages('hierarchical=0&include=13') ;
,其 id 也被硬编码。所以所有页面都显示相同的。我知道我可以为每个硬编码创建一个带有 is_page 的条件,但想法是使其动态。
任何帮助都会很棒!
谢谢,
<?php
$parent = 13;
$args= array(
'parent' => $parent,
'hierarchical' => 0
);
$pages = get_pages('hierarchical=0&include=13') ;
foreach ( $pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li><a href="#' . $new_title . '" title="' . $page->post_title . '">' . $page->post_title . '</a></li>';
}
$child_page_pages = get_pages($args);
foreach ( $child_page_pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li><a href="#' . $new_title . '" title="' . $page->post_title . '">' . $page->post_title . '</a></li>';
}
?>
I'm creating a wordpress theme with parallax features and html5, the home has all the main pages and two different kind of navigations.
Main one, is the navigation for parent pages. So I have About, Projects and Contact.
But each one of this pages has child pages. About page has one child that is below it, but as i cannot access from the main menu, each page that has childs or subpages has the second type of menu.
Also projects, has different pages: "Projects" (main), "Design" (child), Consulting (child), etc.
The second menu that is placed on the right of the page as rounded buttons lists the parent "about" and the child "about 2".
I already figured it out a way to list those and to make the links behave with scroll to (parallax behavior)
But, the way that i have hardcodes the parent page. What i would like to do is to find a way to get dinamically the page ID so it won't be hardcoded, but as the theme works as a only one page site, it's making me get troubles.
Here is the code that i have, could anyone help me to find a way so the code is going to identify the ID of each current page to list the main and the children pages ?
As you see $parent = 13;
is hardcoded, and also it includes $pages = get_pages('hierarchical=0&include=13') ;
with the id hardcoded too. So all the pages are showing the same. I know i can create a conditional with is_page for each hardcoding it, but the idea is to make it dynamic.
Any help will be great!
THanks,
<?php
$parent = 13;
$args= array(
'parent' => $parent,
'hierarchical' => 0
);
$pages = get_pages('hierarchical=0&include=13') ;
foreach ( $pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li><a href="#' . $new_title . '" title="' . $page->post_title . '">' . $page->post_title . '</a></li>';
}
$child_page_pages = get_pages($args);
foreach ( $child_page_pages as $page ) {
$new_title = str_replace( " ", "", strtolower( $page->post_name ) );
echo '<li><a href="#' . $new_title . '" title="' . $page->post_title . '">' . $page->post_title . '</a></li>';
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要获取帖子父页面 ID,您可以使用
get_post_ancestors( $post )
它应该返回父 ID。您可以尝试的另一种方法是全局
$post
。To get post parent page ID's you can use
get_post_ancestors( $post )
it should return the parent ID.Another method you can try is the global
$post
.