简体   繁体   English

的WordPress的:条件标记在CSS的div背景图像?

[英]Wordpress: Conditional tag in css for div background image?

For the meta-data categories on my blog I have that part of the loop in its own div with a background image. 对于我博客上的元数据类别,我将循环的这一部分放在自己的div中并带有背景图片。 I'd like to have the background image change based on the category of that post. 我想根据帖子的类别更改背景图片。 I believe I can do this in css with a set of conditional tags (something along the lines of if category () echo red.png) Any ideas of where to start with this or if it is possible? 我相信我可以使用一组条件标签在CSS中做到这一点(类似于if category()echo red.png的东西)关于从何处开始或是否有可能的任何想法? The site is blog.printstitchandpaste.com and the image to be changed is the little flag/banner behind the category name. 该站点为blog.printstitchandpaste.com,要更改的图像是类别名称后面的小标志/横幅。 Thanks! 谢谢!

In your CSS, you can have something like: 在CSS中,您可以使用类似以下内容的代码:

.category_callout.cat-1-slug {
  background-image: url('category-1-image.jpg');
}
.category_callout.cat-2-slug {
  background-image: url('category-2-image.jpg');
}

And so on. 等等。

In your theme, where you have your .category_callout div, get the categories and echo the slugs. 在您的主题中,您拥有.category_callout div的位置,获取类别并呼应the。 For example: 例如:

<div class="category_callout <?php foreach( get_the_category() as $cat ) { echo $cat->slug . ' '; } ?>">

This will add all the post categories slugs. 这将添加所有帖子类别的标签。 If you want to use only the first category, do something like: 如果只想使用第一类,请执行以下操作:

<?php $cat = get_the_category(); ?>
<div class="category_callout <?php echo $cat[0]->slug; ?> ">

See http://codex.wordpress.org/Function_Reference/get_the_category 参见http://codex.wordpress.org/Function_Reference/get_the_category

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM