Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WordPress-如何做;“当前页面项目”;自定义分类术语列表中的类?_Wordpress - Fatal编程技术网

WordPress-如何做;“当前页面项目”;自定义分类术语列表中的类?

WordPress-如何做;“当前页面项目”;自定义分类术语列表中的类?,wordpress,Wordpress,我有以下代码生成分类术语中的术语列表,然后在每个术语下发布 我希望将一个current page item类添加到当前项中,这样当您在分类术语下的页面上时,它在nav中的相关项将被设置样式。这是我的密码: <?php $terms = get_terms('benefit-cats'); echo "<ul>"; foreach ($terms as $term) { $wpq = array ('taxonomy'=&g

我有以下代码生成分类术语中的术语列表,然后在每个术语下发布

我希望将一个current page item类添加到当前项中,这样当您在分类术语下的页面上时,它在nav中的相关项将被设置样式。这是我的密码:

<?php $terms = get_terms('benefit-cats');
        echo "<ul>";
        foreach ($terms as $term) {
            $wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug,'order'=>'asc','orderby'=>'title');
            $query = new WP_Query ($wpq);
            echo "<li class=".$term->slug."><span class=\"list-item\"><span class=\"text-arrow\">&#9658;</span> ".$term->name."</span>"; //<a href=\"".get_term_link($term->slug, 'benefit-cats')."\"></a>//
            echo "<ul class=\"children\">";
            ?>

            <?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<li><span class="text-arrow">&#9658;</span> <a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php endwhile; endif; wp_reset_query(); ?>

            <?php   
            echo "</ul></li>";
        }
    echo "</ul>";
?>


  • 你可以试试这样的东西。。。 在循环之前指定当前post_id,然后设置条件以查看post循环是否包含post_id

    // before loop
    $page_id = $wp_query->get_queried_object_id();
    
    // replace <li><span class="text-arrow">&#9658;</span> 
    if($page_id ==$query->post->ID ) $class = " current-page-item";
    <li><span class="text-arrow<?php echo $class; ?>">&#9658;</span> 
    
    //循环之前
    $page_id=$wp_query->get_queryed_object_id();
    //替换
  • 和#9658; 如果($page_id==$query->post->id)$class=“当前页面项”;