在两个php文件之间传递值 [英] passing value between two php files

查看:98
本文介绍了在两个php文件之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的wordpress页面,我想在header.php和style.php(php版本的style.css)文件之间传递值。



在标题中。 php我有:

 <?php $ background = get_field('background')['url']; ?> 

检索自定义字段值(已上传的img网址)。



在我的style.php我有我的CSS,但在此之前,我有我的<?php?> 标签,其中我可以定义一些变量使用在样式表。任何想法,如何从header.php检索此值并存储在style.php上的变量?



任何帮助都非常感谢。

解决方案

我正确地跟着你,你实际上想要更多地控制CSS中的背景图片,但需要能够允许用户控制后端来定义它。



在这种情况下,我会在 header.php < head> p>

 < style> 
.background {
background-image:url(<??php echo $ background?>);
}
< / style>

在CSS文件中可以控制元素的其余部分。

$



这是我今天在网站上做的一个现实世界的例子。


$ b

 < style> 
.background {
background-image:url(<?php the_field('background_image')?>);
}
< / style>

我的HTML:

 < div class =background> 
内容来到这里
< / div>

我的CSS:

  .background {
background-size:cover;
background-position:center center;
height:600px;
}
@media only screen and(max-width:40em){
.background {
height:400px;
}
}



如果您正在尝试定义一个GLOBAL变量,那么你把它放在你的变量上面:



<?php global $ background; ?>



然后你可以在任何其他页面上使用$ background(但是如果它在标题中使用,其他页面,因为这是一个模板部分)。


I have my wordpress page where I want to pass value between header.php and style.php(php version of style.css) files.

In header.php I have:

<?php $background = get_field('background')['url']; ?>

Which retrieves custom field value(uploaded img url).

In my style.php I have my css, but before that I have my <?php ?> tag where I can define some variables to use in stylesheet. Any ideas, how to retrieve this value from header.php and store in a variable on style.php?

Any help is very much appreciated, thanks.

解决方案

If I follow you correctly, you're essentially wanting to have more control over the background image in CSS, but need to be able to allow the user control on the backend to define it.

In that case I would do something simple in the header.php <head> like,

<style>
.background {
   background-image: url("<?php echo $background ?>");
}
</style>

in the CSS file you can govern the rest of the control of the element.

Here's a real-world example of what I did today on a site.

In my head:

   <style>
      .background {
          background-image:url(<?php the_field('background_image')?>);
       }
   </style>

My HTML:

<div class="background">
Content goes here
</div>

My CSS:

    .background {
      background-size: cover;
      background-position: center center;
      height: 600px;
    }
    @media only screen and (max-width: 40em){
        .background {
          height: 400px;
        }
     }

If you're attempting to define a GLOBAL variable, then you put this above your variable:

<?php global $background; ?>

Then you can use $background on any other page (but if it's being used in the header, then it's already accessible on other pages since that's a template part).

这篇关于在两个php文件之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆