Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
跨多个页面在CSS文本背景中使用PHP值_Php_Html_Css - Fatal编程技术网

跨多个页面在CSS文本背景中使用PHP值

跨多个页面在CSS文本背景中使用PHP值,php,html,css,Php,Html,Css,我有一种文本样式,它使用页面上的图像在文本上创建该图像的轮廓。例如,下面是文本设置为“联系我们”时的显示方式 CSS <style> .heading-background { background: url(<?php the_field('background'); ?>) center center / cover; -webkit-background-clip: text; -webkit-text-fi

我有一种文本样式,它使用页面上的图像在文本上创建该图像的轮廓。例如,下面是文本设置为“联系我们”时的显示方式

CSS

<style>
    .heading-background {
        background: url(<?php the_field('background'); ?>) center center / cover;
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
</style>

.标题背景{
背景:url()中间/封面;
-webkit背景剪辑:文本;
-webkit文本填充颜色:透明;
}
我使用的是WordPress,背景是通过PHP选择的,这要求我在使用它的每个页面上放置一个样式块。如果我需要进行更改,我必须找到每个样式块并分别更新它们。我不能将其移动到CSS文件中,因为PHP变量是后台显示所必需的

是否可以将该类移动到单个文件中,以发送PHP变量并接收更新后的块?

如果我理解正确,可以将CSS放入名为
background.PHP
的文件中。然后在每个页面上都包含它,但在上面是背景,并将其放在一个php变量中。比如说:

background.php


.标题背景{
背景:url()中间/封面;
-webkit背景剪辑:文本;
-webkit文本填充颜色:透明;
}
page_1.php



然后,无论有多少个页面模板,都要重复上述步骤。下面是关于函数的一些额外信息。

将所有这些样式放在一个php文件中,在该文件中回显此标记,然后在每个页面上包含此php。这样,如果您需要更改任何内容,只需更改包含的php文件。您可以将此块提取到一个单独的php文件中,并将其与include_一起包含一次或require_一起包含到所有页面中。另外,你也可以用php生成css文件,但我认为这对这种情况来说是一种过分的做法。同意评论。CSS变量是一个选项,虽然对IE等的支持不是很好,但如果这不是问题的话,它们相对来说是可以的。没有IE和弱移动支持。这对解决它非常有帮助!我在
get\u template\u part
传递变量时遇到问题,
include(locate\u template('background.php')为我工作。
<style>
    .heading-background {
        background: url(<?php echo $bg_field; ?>) center center / cover;
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
    }
</style>
<?php $bg_field = get_field('background'); ?>
<?php get_template_part('background'); ?>