1.现在function.php里面添加下面的代码
function get_category_root_id($cat) {
$this_category = get_category($cat); // 取得当前分类
while($this_category->category_parent) // 若当前分类有上级分类时,循环
{
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
}
return $this_category->term_id; // 返回根分类的id号
}
2.然后在页面要显示二级分类的地方粘贴下面这段代码即可文章源自爱尚资源教程网-https://www.23jcw.net/2435.html
<?php
if(is_single()||is_category()){
if(get_category_children(get_category_root_id(the_category_ID(false)))!= "" ){
echo '<div class="header-sub">';
echo '<ul class="chind-cat">';
echo wp_list_categories("child_of=".get_category_root_id(the_category_ID(false)). "&depth=0&hide_empty=0&title_li=&orderby=id&order=ASC");
echo '</ul>';
echo '</div>';
}
}
?>
这个函数的功能就是在分类页和文章页显示当前分类的子分类(二级分类)。文章源自爱尚资源教程网-https://www.23jcw.net/2435.html
另外style如下:文章源自爱尚资源教程网-https://www.23jcw.net/2435.html
<style type="text/css">.header-sub {
width: 1200px;
/*margin: 0 auto;*/
}
.header-sub ul {
list-style: none;
margin: 0 0 5px 0;
overflow: hidden;
zoom: 1;
}
.chind-cat a {
background: #fff;
text-align: center;
line-height: 36px;
font-size: 20px;
padding: 0 5px;
display: block;
white-space: nowrap;
word-wrap: normal;
text-overflow: ellipsis;
overflow: hidden;
border: 1px solid #ddd;
border-radius: 2px;
transition-duration: .5s;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
border-radius: 5px;
margin: 0 2px;
}
.chind-cat a:hover {
background: #3690cf;
color: #fff !important;
border: 1px solid #3690cf;
transition: all 0.1s ease-in 0s;
}
.chind-cat li {
width: 14.2857%;
transition-duration: .5s;
/*margin: 3px 0px;*/
margin: 3px 5px 3px 0px;
float: left;
}
@media only screen and (min-width: 360px) and (max-width: 767px) {
.header-sub {
width: 100%;
margin: 0 auto;
}
.chind-cat li {
width: 50%;
transition-duration: .5s;
margin: 3px 0px;
float: left;
}
}
</style>
文章源自爱尚资源教程网-https://www.23jcw.net/2435.html文章源自爱尚资源教程网-https://www.23jcw.net/2435.html 相关文章
版权声明:文章图片资源来源于网络,如有侵权,请留言删除!!!
