如何输出根据分类罗列文章,当前页面高亮显示
我之前有写过类似这个功能,这次写的全一点,高亮显示实现功能也是用到这个$uri=explode('/',$_SERVER['REQUEST_URI']);
我贴一下代码,你们可以参考一下,个人暂时未找到更好的方法,有什么好的方法,可以在下面评论,我们探讨一下。
$uri=explode('/',$_SERVER['REQUEST_URI']);
<?php
$categories = get_categories("hierarchical=1&taxonomy=video&hide_empty=0");
$cats=array();
foreach($categories as $tmpcat){
if($tmpcat->parent>0){
$cats[$tmpcat->parent]['child'][$tmpcat->term_id]['name']=$tmpcat->name;
$cats[$tmpcat->parent]['child'][$tmpcat->term_id]['term_id']=$tmpcat->term_id;
}else{
$cats[$tmpcat->term_id]['name']=$tmpcat->name;
$cats[$tmpcat->term_id]['term_id']=$tmpcat->term_id;
}
}
ksort($cats);
foreach($cats as $key=>$val){?>
<li class="cat-item">
<i class="fas fa-angle-down"></i><a><?php echo $val["name"]; ?></a>
<ul class="children">
<?php foreach($val["child"] as $cat){ ?>
<li class="cat-item">
<a<?php if (ucwords(str_replace('-',' ',$uri[3])) == $cat["name"] ) {echo ' class="opeth"';}?>><?php echo $cat["name"]; ?></a>
<?php
$posts_array = get_posts(
array(
'posts_per_page' => 3,
'post_type' => 'videos',
'tax_query' => array(
array(
'taxonomy' => 'video',
'field' => 'term_id',
'terms' => $cat['term_id'],
)
)
)
);
if ($posts_array){?>
<ul class="children">
<?php
foreach( $posts_array as $post ){
$youtube = substr(basename(get_field('add_link')),8);
$a = substr(strrchr(rtrim(get_permalink(), '/'), '/'), 1);?>
<li<?php if ($uri[4] == $a ) {echo ' class="opeth"';} ?>>
<div class="list-video">
<div class="col-md-4">
<iframe width="100%" height="45px" src="https://www.youtube.com/embed/<?php echo $youtube;?>" frameborder="0" showinfo="0"></iframe>
</div>
<div class="col-md-8">
<a href="<?php echo get_permalink(); ?>"><?php echo get_the_title(); ?></a>
</div>
<div class="clearfix"></div>
</div>
</li>
<?php } ?>
</ul>
<?php } ?>
</li>
<?php } ?>
</ul>
</li>
<?php } ?>