【发布时间】:2020-07-07 06:18:33
【问题描述】:
我的网站上有这个部分:
这是在我的主页上,通过称为产品的自定义帖子类型的项目。
每个产品都有自己的产品颜色,我允许用户从高级自定义字段颜色选择器中进行选择。
我使用 CSS :before 和 :after 创建了箭头(一个用于直线,一个用于箭头),因此我可以使用产品颜色 ACF 为它们着色。
这意味着我必须在模板内应用颜色内联。但由于我使用了伪类,我不相信我可以为它们内联样式。
为了解决这个问题,我在循环中添加了一个样式块...这似乎有效,但它只采用黄色,因为我认为它是它找到的第一种颜色...
有没有办法解决这个问题?不知道是不是太复杂了……
这是与 CSS 文档分开的 CSS:
.products-item-inner .arrow-right:after {
content: "";
border: solid;
/* border-color: <?php the_field('product_colour'); ?>; */
border-width: 0 2px 2px 0;
display: inline-block;
vertical-align: middle;
padding: 5px;
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
margin-left:-12px;
}
.products-item-inner .arrow-right:before {
width: 30px;
height: 2px;
/* background-color: <?php the_field('product_colour'); ?>;*/
content: "";
display: inline-block;
vertical-align: middle;
}
这是添加了 CSS <style> 内联的模板循环:
<div class="container">
<div class="row">
<?php
$args = array(
'post_type' => 'products',
'posts_per_page' => 9999,
'orderby' => 'none'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- <a href="<?php the_permalink(); ?>"> -->
<div class="col-lg-4 products-item-outer">
<div class="col-12 products-item-inner">
<style type="text/css">
.products-item-inner .arrow-right:after { border-color: <?php the_field('product_colour'); ?>; }
.products-item-inner .arrow-right:before { background-color: <?php the_field('product_colour'); ?>; }
</style>
<div class="logo">
<?php
$image = get_field('logo');
if( !empty( $image ) ): ?>
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
<?php endif; ?>
</div>
<div class="excerpt"><p><?php the_field('excerpt_text'); ?></p></div>
<div class="read-more-link"><a href="<?php the_permalink(); ?>">Read More</a><span class="arrow-right"></span></div>
</div>
</div>
<!-- </a> -->
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
感谢收看 :)
【问题讨论】:
-
您可以使用 CSS 自定义属性,还是需要支持旧版浏览器?
-
那是评论 - 我想我会同意那些我们说我们不支持 Internet Explorer 并且如果我们支持它只会改变颜色 - 虽然我不知道很多关于他们的事吗?
标签: php css advanced-custom-fields custom-post-type pseudo-element