简体   繁体   English

WordPress 将元素添加到 1 个特定菜单项

[英]WordPress add element to 1 specific menu item

I have a wordpress menu and in this menu there is a page Products I want to add a small counter to this menu item.我有一个 wordpress 菜单,在这个菜单中有一个页面Products我想向这个菜单项添加一个小计数器。 But I don't know how to edit that specific item.但我不知道如何编辑该特定项目。

I count the products that I have but I am not sure where to echo it.我数了数我拥有的产品,但我不知道在哪里回应它。

<?php
$countProducts = wp_count_posts('product');
?>

<section role="banner" style="min-height: 150px;">
    <header id="header" class="not-homepage-header">
        <div class="header-content clearfix"> <a class="logo" href="#"><img src="<?= get_template_directory_uri(); ?>/dist/images/logo.png" alt=""></a>
            <nav class="navigation" role="navigation">
                <?php
                if (has_nav_menu('primary_navigation')) :
                    wp_nav_menu(
                        array(
                            'theme_location'    => 'primary_navigation',
                            'depth'             => 2,
                            'container'         => 'ul',
                            'container_class'   => 'collapse navbar-collapse',
                            'container_id'      => 'bs-example-navbar-collapse-1',
                            'menu_class'        => 'primary-nav mr-auto',
                            'fallback_cb'       => 'WP_Bootstrap_Navwalker::fallback',
                            'walker'            => new WP_Bootstrap_Navwalker()
                        )
                    );
                endif;
                ?>
                <li>
                    <?php echo $countProducts->publish; ?>
                </li>
            </nav>
            <a href="#" class="nav-toggle">Menu<span></span></a> </div>
    </header>
</section>

I currently do this but obviously it places it at the end of the menu.我目前这样做,但显然它放在菜单的末尾。 I would like to place the counter in a <span></span> in the li of the menu item.我想将计数器放在菜单项li<span></span>中。

I suggest you to use pseudo element and you have to only modify the CSS of the menu element.我建议你使用伪元素,你只需要修改菜单元素的 CSS。 You may have something like this :你可能有这样的事情:

 .menu-item:after { content:" 3" /* then add the style needed*/ }
 <ul> <li>Home</li> <li class="menu-item">product</li> </ul>

So your code will probably look like this :所以你的代码可能看起来像这样:

<?php
$countProducts = wp_count_posts('product');
?>

<style>
.class_of_menu_item:after {
  content:"<?php echo $countProducts; ?>";
}
</style>
<section role="banner" style="min-height: 150px;">
    <header id="header" class="not-homepage-header">
        <div class="header-content clearfix"> <a class="logo" href="#"><img src="<?= get_template_directory_uri(); ?>/dist/images/logo.png" alt=""></a>
            <nav class="navigation" role="navigation">
                <?php
                if (has_nav_menu('primary_navigation')) :
                    wp_nav_menu(
                        array(
                            'theme_location'    => 'primary_navigation',
                            'depth'             => 2,
                            'container'         => 'ul',
                            'container_class'   => 'collapse navbar-collapse',
                            'container_id'      => 'bs-example-navbar-collapse-1',
                            'menu_class'        => 'primary-nav mr-auto',
                            'fallback_cb'       => 'WP_Bootstrap_Navwalker::fallback',
                            'walker'            => new WP_Bootstrap_Navwalker()
                        )
                    );
                endif;
                ?>
                <li>
                    <?php echo $countProducts->publish; ?>
                </li>
            </nav>
            <a href="#" class="nav-toggle">Menu<span></span></a> </div>
    </header>
</section>

You only need to find the class of your menu element and since it's a wordpress site you should be able to find a unique one (or maybe an id too).你只需要找到你的菜单元素的类,因为它是一个 wordpress 站点,你应该能够找到一个唯一的(或者也可能是一个 id)。

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

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