我解释了我的情况:
我有一个博客,在主页上显示了最新的帖子。
对于每个帖子类别,我都通过css设置了一个特定的颜色和图标。
例如:对于宠物类别:
article.category-pets .meta-category a {
color: white;
float: left;
padding: 2px;
padding-left: 25px;
background: url(http://mywebsite.com/wp-content/uploads/2015/02/pets-30_30.png) no-repeat left;
background-size: 15px 15px;
background-color: #a2591e;
background-position-x: 5px;
padding-right: 5px;
}
这非常有效。
我用来显示类别名称的循环是:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
我想要实现的是:
仅显示帖子的子类别(如果帖子链接到子类别及其父类别),如果帖子设置为仅显示一个父类别,则显示此父类别。
这个循环的效果不是很好,因为如果我的帖子链接了,例如:
父类别AChild类别A1
和
父类别B子类别B1
它会把事情搞砸的,在我的例子中,它会显示子类别名称(如B1),但会显示父类别A的图标和背景色。
它应该只显示子类别A的名称-背景色和图标A/而不是B。
我不能使用循环来显示唯一的子类别或唯一的父类别,因为有些帖子(有数千篇)只设置为父类别。
我真的很喜欢这方面的任何帮助,会很可爱的!
感谢您的宝贵时间:)
--编辑--我添加生成的文章:
这是一个工作正常的示例,因为帖子只设置为一个父类别及其子类别
<article class="post-3546 post type-post status-publish format-standard has-post-thumbnail hentry category-pet-of-the-week category-pets vce-post vce-lay-c">
<h2 class="entry-title"><a href="http://mywebsite.com/pets/pet-of-the-week/pets-sale-5th-feb/" title="Pets For Sale On (5th Feb)">Pets For Sale On (5th Feb)</a></h2>
<div class="meta-image">
<a href="http://mywebsite.com/pets/pet-of-the-week/pets-sale--5th-feb/" title="Pets For Sale On (5th Feb)">
<img width="292" height="195" src="http:/mywebsite.com/wp-content/uploads/2015/02/siberian-husky.jpg" class="attachment-vce-lay-b wp-post-image" alt="Siberian Husky For Sale" /> </a>
<span class="meta-category">
<a href="http://mywebsite.com/category/pets/pet-of-the-week/">Pet Of The Week</a>
</span>
</div>
<header class="entry-header">
<div class="entry-meta"><div class="meta-item views">26 Views</div><div class="meta-item date"><span class="updated">2 weeks ago</span></div></div>
</header>
<div class="entry-content">
<p>As another month rolls in, with it comes a fresh supply of adorable pets for your viewing pleasure...</p>
</div>
</article>
这里是它不能正常工作的时候,因为帖子附加到3个父类别和他们的一些子类别,所以它混淆了显示子类别的名称,但背景颜色和另一个父类别的图标。
<article class="post-3107 post type-post status-publish format-standard has-post-thumbnail hentry category-bedroom-2 category-business-and-finance category-childrens category-home-garden category-london category-money-saving category-others category-selling-and-advertising tag-cleaning tag-clutter tag-declutter tag-home tag-house tag-london tag-sell tag-space tag-summer tag-to-do-list vce-post vce-lay-c">
<h2 class="entry-title"><a href="http://mywebsite.com/others/london/home/" title="A Guide to Clearing Out the Clutter : You Can De-junk Your House in a Weekend">A Guide to Clearing Out the Clutter : You...</a></h2>
<div class="meta-image">
<a href="http://mywebsite.com/others/london/home/" title="A Guide to Clearing Out the Clutter : You Can De-junk Your House in a Weekend">
<img width="225" height="195" src="http://mywebsite.com/wp-content/uploads/2014/07/AM-quotes-laundryTsunami-final-Edited.jpg" class="attachment-vce-lay-b wp-post-image" alt="AM-quotes-laundryTsunami-final - Edited" /> </a>
<span class="meta-category">
<a href="http://mywebsite.com/category/home-garden/bedroom-2/">Bedroom</a>
</span>
</div>
<header class="entry-header">
<div class="entry-meta"><div class="meta-item views">7 Views</div><div class="meta-item date"><span class="updated">7 months ago</span></div></div>
</header>
<div class="entry-content">
<p>OUT with the Old… A Guide to Clearing Out the Clutter : You Can De-junk Your House in a...</p>
</div>
</article>
----编辑-2相关代码---
<article <?php post_class(\'vce-post vce-lay-c\'); ?>>
<?php if($fimage = vce_featured_image(\'vce-lay-b\')): ?>
<h2 class="entry-title"><a href="<?php echo esc_url(get_permalink()); ?>" title="<?php echo esc_attr(get_the_title()); ?>"><?php echo vce_get_title(\'lay-c\'); ?></a></h2>
<div class="meta-image">
<a href="<?php echo esc_url(get_permalink()); ?>" title="<?php echo esc_attr(get_the_title()); ?>">
<?php echo $fimage; ?>
<?php if($icon = vce_post_format_icon(\'lay_c\')) :?>
<span class="vce-format-icon">
<i class="fa <?php echo $icon; ?>"></i>
</span>
<?php endif; ?>
</a>
<?php if( vce_get_option(\'lay_c_cat\')) : ?>
<?php if (function_exists(\'z_taxonomy_image_url\')) echo z_taxonomy_image_url(); ?>
<span class="meta-category">
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
</span>
<?php endif; ?>
</div>
<?php endif; ?>
<header class="entry-header">
<div class="entry-meta"><?php echo vce_get_meta_data(\'lay-c\'); ?></div>
</header>
<?php if( vce_get_option(\'lay_c_excerpt\')) : ?>
<div class="entry-content">
<p><?php echo vce_get_excerpt(\'lay-c\'); ?></p>
</div>
<?php endif; ?>
</article>
最合适的回答,由SO网友:geomagas 整理而成
对于您发布的示例,文章的样式是根据category-selling-and-advertising
最后定义的(在category-*
类)中的class
属性
您需要拦截的功能post_class()
要更改这些类的显示顺序。这个post_class
筛选器将为您执行以下操作:
add_filter(
\'post_class\',
function($classes) // $classes is an array
{
$cats=get_the_category();
$master_cat=$cats[0]->slug;
if($k=array_search("category-$master_cat",$classes))
{
unset($classes[$k]);
$classes[]="category-$master_cat";
}
return $classes;
}
);
以上是一个相当幼稚的解决方案,但我认为这是一个很好的起点。