如何在wordpress中添加类别菜单的活动状态?

时间:2014-10-08 03:01:17

标签: php wordpress

我为类别菜单创建了一个模板。点击"类别",根据类别重新加载页面显示该类别中的帖子。我需要激活类别菜单。怎么办?

类别-menu.php

<div class="row text-center" >
    <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
        <?php
            $menu_item=wp_get_nav_menu_items( 'Category Menus', $args );
            foreach ($menu_item as $menu) {  
        ?>
        <div class="col-lg-3 col-sm-12 col-xs-12 col-md-3 menu-padding">
            <div class="blog-menu-items cat-menu-color">
                <a href="<?php echo $menu->url; ?>">
                    <?php echo $menu->title; ?>
                </a>
            </div>
        </div>
        <?php } ?>
    </div>
</div>

Category.php

<?php get_template_part( 'category', 'menus' ); ?>

<?php $cat_title = strtoupper(get_cat_name($category_id)); ?>

<div class="row" style="padding-top:5%;">
    <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
        <a href="<?php echo home_url() ?>" class="back-to-blog">
            <i class="icon-chevron-left"></i>
            <?php echo __( 'Back to Home', 'tiny_theme') ?>
        </a>
    </div>
</div>
<div class="row popular-posts">
    <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
        <div class="col-lg-8 col-sm-6 col-xs-6 col-md-8 popular-posts-header pull-left">
            <h4>POSTS ON <?php echo $cat_title ?></h4>
        </div>
    <?php if ($query1->max_num_pages > 1) { ?>
        <div class="col-lg-4 col-sm-6 col-xs-6 col-md-4 pull-right" id="popular-post-pager" >
                    <?php echo paginate_links( $pag_args1 );?>
        </div>
    <?php } ?>
    </div>
    <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
    <?php
        if ($query1->have_posts()) {
            while ($query1->have_posts()) {
                $query1->the_post();
    ?>
        <h4 style="padding-top:5px;">
            <a href="<?php the_permalink(); ?>"><?php echo the_title(); ?></a>
        </h4>
        <p> <?php the_excerpt(10); ?></p>
    <?php   }
        }
        wp_reset_query();
    ?>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

您可以尝试在当前类别ID和object_id之间进行比较,该object_id包含链接所关联对象的ID,类似这样的

(未经测试)

<div class="row text-center" >
    <div class="col-md-12 col-xs-12 col-sm-12 col-lg-12">
        <?php
            global $post;
            $menu_item=wp_get_nav_menu_items( 'Category Menus', $args );
            foreach ($menu_item as $menu) {  
              $currentClass = '';
              if($post->ID == $menu->object_id)
                $currentClass = 'current-menu-item';
        ?>
        <div class="col-lg-3 col-sm-12 col-xs-12 col-md-3 menu-padding">
            <div class="blog-menu-items cat-menu-color">
                <a class="<?php echo $currentClass; ?>" href="<?php echo $menu->url; ?>">
                    <?php echo $menu->title; ?>
                </a>
            </div>
        </div>
        <?php } ?>
    </div>
</div>