关于 WordPress 导航的帮助
我目前正在我的 WordPress 主题中执行以下操作,
<?php
$pagelist = wp_list_pages( array(
'include' => '154, 136',
'title_li' => '',
'sort_column' => 'ID',
'sort_order' => 'DESC',
'depth' => '0',
'echo' => false
));
if($post->post_type == "casestudy") {
$args = array( 'post_type' => 'casestudy');
$case_studies = new WP_Query( $args );
$casestudylist = "";
foreach ($case_studies->posts as $k => $v) {
$active = "";
if($v->post_status == "publish") {
if($v->ID == $post->ID)
{
$active = "current_page";
}
$casestudylist .= "<li class='subnav ".$active."'><a href=".$v->guid.">". substr($v->post_title, 0, strpos($v->post_title, ':')) ."</a></li>";
}
}
}
$testimonials = wp_list_pages( array(
'include' => '318',
'title_li' => '',
'sort_column' => 'ID',
'sort_order' => 'DESC',
'depth' => '1',
'echo' => false
));
//die(print_r($casestudylist));
$concatenatedlist = $pagelist . $casestudylist . $testimonials;
?>
<aside class="secondary">
<nav>
<ul>
<?php echo $concatenatedlist; ?>
</ul>
</nav>
</aside>
这构建了一个导航,并给人留下这样的印象:页面有一些使用自定义帖子类型构建的子级。问题是,如果我正在查看来自 $casestudylist
的帖子,我还需要以某种方式将活动类添加到来自 $pagelist
的第一个链接上,甚至虽然两者无关。我不介意它是否是一个 jQuery 解决方案。基本上,如果我的 casestudylist
中的某个元素处于活动状态,我需要将案例研究链接也标记为活动状态。
这是 PHP 创建的 HTML。
<aside class="secondary">
<nav>
<ul>
<li class="page_item page-item-154">
<a href="/clients">Clients</a>
</li>
<li class="page_item page-item-136">
<a href="/case-study">Case Study</a
</li>
<li class="subnav current_page">
<a href="/?post_type=casestudy&p=161">Brenntag</a>
</li>
<li class="subnav ">
<a href="/?post_type=casestudy&p=104"></a>
</li>
<li class="subnav ">
<a href="?post_type=casestudy&p=65"></a>
</li>
<li class="page_item page-item-318">
<a href="/testimonials">Testimonials</a>
</li>
</ul>
</nav>
</aside>
I am currently doing the following in my wordpress theme,
<?php
$pagelist = wp_list_pages( array(
'include' => '154, 136',
'title_li' => '',
'sort_column' => 'ID',
'sort_order' => 'DESC',
'depth' => '0',
'echo' => false
));
if($post->post_type == "casestudy") {
$args = array( 'post_type' => 'casestudy');
$case_studies = new WP_Query( $args );
$casestudylist = "";
foreach ($case_studies->posts as $k => $v) {
$active = "";
if($v->post_status == "publish") {
if($v->ID == $post->ID)
{
$active = "current_page";
}
$casestudylist .= "<li class='subnav ".$active."'><a href=".$v->guid.">". substr($v->post_title, 0, strpos($v->post_title, ':')) ."</a></li>";
}
}
}
$testimonials = wp_list_pages( array(
'include' => '318',
'title_li' => '',
'sort_column' => 'ID',
'sort_order' => 'DESC',
'depth' => '1',
'echo' => false
));
//die(print_r($casestudylist));
$concatenatedlist = $pagelist . $casestudylist . $testimonials;
?>
<aside class="secondary">
<nav>
<ul>
<?php echo $concatenatedlist; ?>
</ul>
</nav>
</aside>
This builds a navigation, and gives the impression that a page has some children that are built with custom post types. The problem is that if I'm looking at a post from the $casestudylist
I need to somehow also add an active class onto the first link that comes from $pagelist
, even though the two are unrelated. I don't mind if it is a jQuery solution. Basically, if an element in my casestudylist
is active, I need to have the case study link marked as active also.
Here is the HTML the PHP creates.
<aside class="secondary">
<nav>
<ul>
<li class="page_item page-item-154">
<a href="/clients">Clients</a>
</li>
<li class="page_item page-item-136">
<a href="/case-study">Case Study</a
</li>
<li class="subnav current_page">
<a href="/?post_type=casestudy&p=161">Brenntag</a>
</li>
<li class="subnav ">
<a href="/?post_type=casestudy&p=104"></a>
</li>
<li class="subnav ">
<a href="?post_type=casestudy&p=65"></a>
</li>
<li class="page_item page-item-318">
<a href="/testimonials">Testimonials</a>
</li>
</ul>
</nav>
</aside>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要在 jQuery 中执行此操作,您可以说:
但是对帖子 ID 进行硬编码并不是很可靠,也不能依赖 jQuery 来应用 WP 应该提供的样式。
这里有一些值得参考的想法正在研究,但我的直觉反应是执行以下操作之一:
To do it in jQuery you could say:
But hard-coding the post ID isn't very robust, nor is relying on jQuery to apply a style that WP should be serving.
There are a few ideas here worth looking into, but my gut reaction is to do one of the following:
观察:
$casestudylist
应在if
之前设置为“”。if($post->post_type == "casestudy")
部分。$casestudylist
包装在中以便嵌套它。
Observations:
$casestudylist
should be set to "" before theif
.if($post->post_type == "casestudy")
section.$casestudylist
in<ul>
in order to nest it.