关于 WordPress 导航的帮助

发布于 2024-11-28 00:48:15 字数 2799 浏览 0 评论 0原文

我目前正在我的 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&amp;p=161">Brenntag</a>
                    </li>
                    <li class="subnav ">
                         <a href="/?post_type=casestudy&amp;p=104"></a>
                    </li>
                    <li class="subnav ">
                         <a href="?post_type=casestudy&amp;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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里寻她 2024-12-05 00:48:15

要在 jQuery 中执行此操作,您可以说:

if($('li.subnav.current_page').length>0) {
    $('li.page-item-136').addClass('current_page');
}

但是对帖子 ID 进行硬编码并不是很可靠,也不能依赖 jQuery 来应用 WP 应该提供的样式。

这里有一些值得参考的想法正在研究,但我的直觉反应是执行以下操作之一:

  • 用具有父子关系的页面替换页面和自定义帖子
  • 创建自定义分类法以包含所有涉及的页面,并更改主题以显示它。

To do it in jQuery you could say:

if($('li.subnav.current_page').length>0) {
    $('li.page-item-136').addClass('current_page');
}

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:

  • Substitute pages with parent-child relationships for the page and custom posts
  • Create a custom taxonomy to envelop all the pages involved, and alter your theme to display it.
肤浅与狂妄 2024-12-05 00:48:15

观察:

  1. 为了简洁起见,$casestudylist 应在 if 之前设置为“”。
  2. 没有迹象表明您曾经进入过 if($post->post_type == "casestudy") 部分。
  3. 如果是的话,您可能希望将 $casestudylist 包装在
      中以便嵌套它。

Observations:

  1. For cleanliness sake, $casestudylist should be set to "" before the if.
  2. There is no indication you are ever getting into the if($post->post_type == "casestudy") section.
  3. And if you were, you would probably like to wrap the $casestudylist in <ul> in order to nest it.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文