简体   繁体   English

将活动/当前类别添加到子菜单自定义帖子标题

[英]Add active/current class to submenu custom post title

this is the code that I use to create a submenu that list all custom post I create in wordpress: 这是我用来创建子菜单的代码,该子菜单列出了我在wordpress中创建的所有自定义帖子:

    <ul class="submenu">
        <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
        <!-- List post types -->
        <?php
            $the_query = new WP_Query( 'post_type=artworks_post' );
            // The Loop
            while ( $the_query->have_posts() ) : $the_query->the_post();
            echo '<li id="submenu_link" class="submenu_item_link">';
            echo '<a  href="' .get_permalink(). '" > ';
            the_title();
            echo '</a>';
            echo '</li>';
            endwhile;
            // Reset Post Data
            wp_reset_postdata();
        ?> 
        <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
    </ul>

For example my current post is artworks_post/project-coke but it doesn´t add current/active class to the title of it in the submenu (Project Coke title). 例如,我当前的帖子是Artworks_post / project-coke,但是它没有在子菜单中的项目标题(Project Coke标题)中添加当前/活动类。

How can I add an active/current class to the title of the current post in the submenu? 如何在子菜单中的当前帖子标题中添加活动/当前类别?

The submenu is constructed that way to get the custom post... 子菜单是通过这种方式构造的,以获取自定义帖子...

Try my code ? 试试我的代码? :

Put this code in your functions.php 将此代码放在您的functions.php中

function if_current($s) {
    global $wp_query,$post;
    $current    = $wp_query->get_queried_object_id();
    $post_id    = $post->ID;
    if($current==$post_id){echo $s;}
}

So edit your code like : 因此,像这样编辑代码:

<ul class="submenu">
    <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
    <!-- List post types -->
    <?php
        $the_query = new WP_Query( 'post_type=artworks_post' );
        // The Loop
        while ( $the_query->have_posts() ) : $the_query->the_post();
        echo '<li id="submenu_link" class="submenu_item_link ';
        if_current('current');
        echo '">';
        echo '<a  href="' .get_permalink(). '" > ';
        the_title();
        echo '</a>';
        echo '</li>';
        endwhile;
        // Reset Post Data
        wp_reset_postdata();
    ?> 
    <img src="<?php bloginfo('template_directory'); ?>/images/submenu.png" alt="submenu" width="62" height="1" />
</ul>

Result : 结果:

<li id="submenu_link" class="submenu_item_link current">
<a href="http://webkunst.comeze.com/test/artworks_post/project-coke/"> Project Coke</a>
</li>

PS: you can add whatever if_current('text') in loop in your theme, This will show when $post->ID == $wp_query->get_queried_object_id() (if current) PS:您可以在主题中循环添加任何if_current('text') ,这将在$post->ID == $wp_query->get_queried_object_id() (如果当前)

So you can use class current in your CSS 因此,您可以在CSS中使用当前

You need to check which page (or category) you are currently on and add additional class to the submenu item which is active. 您需要检查当前正在哪个页面(或类别),并将其他类添加到活动的子菜单项中。 You can use get_the_ID() to retrieve the ID of the current element in the WP loop. 您可以使用get_the_ID()来检索WP循环中当前元素的ID。

So before this line: 因此,在此行之前:

echo '<li id="submenu_link" class="submenu_item_link">';

You would need to add a check if the current post id matches the post id and add a class to the li element which will show that. 您需要检查当前的帖子ID是否与帖子ID相匹配,并向li元素添加一个类来显示该帖子。

I would suggest you use the WordPress Menu implementation. 我建议您使用WordPress菜单实现。 (Infos here and here ) 这里这里的信息

It's a nice and easy "How it should be" approach for menu creation in WordPress and, for example, marks automatically the active items once you've used the wp_nav_menu function. 这是在WordPress中创建菜单的一种很好且很容易的“应有的方式”,例如,一旦使用wp_nav_menu函数,就会自动标记活动项。

If you feel forced to stick to your approach, you ofc have to check if the current displayed post is equal to one of the posts you display in your loop and echo class like current or active. 如果您被迫坚持使用自己的方法,则必须检查当前显示的帖子是否等于您在循环和回显类(如当前或活动)中显示的帖子之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM