我有一种文本样式,它使用页面上的图像在文本上创建该图像的轮廓。例如,以下是文本设置为“联系我们”时的显示方式。
CSS
<style>
.heading-background {
background: url(<?php the_field('background'); ?>) center center / cover;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
</style>
我正在使用WordPress,并且通过PHP选择背景,这要求我在每个使用它的页面上放置一个样式块。如果我需要进行更改,我必须找到每个样式块并单独更新它们。我无法将其移动到CSS文件,因为PHP变量是显示背景所必需的。
是否可以将此类移动到单个文件中以发送PHP变量并接收更新的块?
答案 0 :(得分:2)
如果我理解正确,您可以将CSS放在名为background.php
之类的文件中。然后在每个页面上包含它,但在它上面获取背景并将其放在php变量中。所以像这样:
<强> background.php 强>
<style>
.heading-background {
background: url(<?php echo $bg_field; ?>) center center / cover;
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
}
</style>
<强> page_1.php 强>
<?php $bg_field = get_field('background'); ?>
<?php get_template_part('background'); ?>
然后重复您拥有的许多页面模板。以下是有关get_template_part()
功能的一些奖励信息。