一周以来我有点麻烦。。我可不喜欢这样!
我解释我的情况:
我已经为帖子类别设置了样式。
(背景色和小图像背景)。
例如:。
父母A子女1子女2子女3
父B-子1-子2
父C-子1-子2
我使用的循环是:
<a href="#" class="<?php $category = get_category($cat);
echo $category->category_nicename;?>"><?php single_cat_title(\'\') ?></a>
当我在parent\\u category页面上时,它可以很好地工作,它带来了我通过css设置的样式,使用category\\u slug作为一个类。(类别上的css示例:)
article.category-business-and-finance .meta-category2 a.business-and-finance {
color: white;
float: left;
padding: 2px;
padding-left: 25px;
background: url(myimg) no-repeat left ;
background-size: 15px 15px;
background-color: #c3bc19 ;
background-position-x: 5px;
padding-right: 5px;
}
然而,当我转到child\\u类别的category\\u页面时,它会显示不同类别的样式。因为一些帖子同时设置为多个PEL parent\\u类别和多个chld类别。我不想为每个child\\u类别创建css,因为将来会添加很多子类别。我想实现的是child\\u类别,将其当前的parent\\u类别slug的类。
如何实现这一目标?
我尝试了这个循环:
<a href="#" class="<?php $parentscategory ="";
foreach((get_the_category()) as $category) {
if ($category->category_parent == 0) {
$parentscategory .= \' \' . $category->slug . \' \';
}
}
echo substr($parentscategory,0,-2);?>"><?php single_cat_title(\'\') ?></a>
这会将所有parent\\u类别中的类添加到帖子。。有没有办法只到目前为止?(例如,我在父A的Child-1分类页面上发了即时消息,但该帖子也附加在父B和父C中。它给我带来了父A B和父C的类,现在我只想附加父A的类,作为父A页面的子类别上的即时消息)。
我该怎么做?我应该尝试当前的\\u parent\\u类别吗?cat\\U祖先?
谢谢你花了这么多时间,