简体   繁体   English

如何使用PHP设置HTML元素的样式

[英]How to style HTML element with PHP

I hope you can help me with this code below. 希望您可以在下面的代码中为我提供帮助。 So I don't know how to style element .img-box with php code. 所以我不知道如何用php代码设置元素.img-box的样式。

this is original code from file: 这是来自文件的原始代码:

<?php hc_echo($img,'<div class="col-md-4">
<div class="img-box">
<img src="','" alt="" /></div></div>' ); 
?>

and I have added this: 并且我添加了这个:

style="esc_attr($Y_NOW["custom_imgbox_style"]);"

so it looks like this: 所以看起来像这样:

<?php hc_echo($img,'<div class="col-md-4">
<div class="img-box" style="esc_attr($Y_NOW["custom_imgbox_style"]);">
<img src="','" alt="" /></div></div>' ); 
?>

but it is not working. 但它不起作用。

I am still a beginner and I no longer know what to do next. 我仍然是一个初学者,我不再知道下一步该怎么做。

I need to have ability to add style for .img-box element. 我需要有能力为.img-box元素添加样式。

Just i dont know how to embed style for .img-box. 我只是不知道如何为.img-box嵌入样式。

Also there is other solution like this: 也有其他类似的解决方案:

<?php
echo '<style type="text/css">
        .img-box {

        }
        </style>'; ?>

but again how to add php variable inside ? 但是再次如何在里面添加php变量?

Thank you 谢谢

You can do: 你可以做:

<?php
echo '<style type="text/css">
        .img-box {
           width: '. $var . ';
        }
        </style>'; ?>

Basically, whenever you need to write anything which is php related in html code just put it in the php tags, like this: 基本上,每当您需要用html代码编写与php相关的任何东西时,只需将其放在php标签中,如下所示:

<?php 

$user = 'Jeff';

?>

<h1><?php echo 'Hey ' . $user . ', how are you?'; ?></h1>

So this will output following text: 因此,这将输出以下文本:

Hey Jeff, how are you? 嘿,杰夫,你好吗?

It's hard to tell why your code is not working, without seeing the code of "hc_echo" function and knowledege of what this function expects as parameters. 很难不看代码“ hc_echo”功能的代码,也不知道该函数作为参数的原因,就很难说出代码为什么不起作用。

Seeing that you are trying to use "esc_attr", which is wordpress related function, I assume you have installed a plugin which uses "hc" prefix for it's functions (because it is a good practice when you are creating wordpress plugins - use your own prefix for that plugin functions) and now you are trying to modify it's code. 看到您正在尝试使用与wordpress相关的功能“ esc_attr”,我假设您已经安装了一个使用“ hc”前缀作为其功能的插件(因为在创建wordpress插件时这是一个好习惯-使用您自己的该插件函数的前缀),现在您正在尝试修改其代码。

What you can try (though I do not recommend doing that - explanation below) is adding style this way: 您可以尝试的方法(尽管我不建议您这样做-以下说明)是通过以下方式添加样式的:

style="<?php esc_attr($Y_NOW["custom_imgbox_style"]); ?>"

This is basically what I mentioned before. 这基本上是我之前提到的。 Since "esc_attr" is a wordpress php function, and you want to put it into html attribute, you have to use php tags. 由于“ esc_attr”是wordpress的php函数,并且您想将其放入html属性,因此必须使用php标签。 Otherwise it will just be a plain text, not an execution of the function. 否则,它将只是纯文本,而不是函数的执行。

I DO NOT recommend doing that though, because this is basically adding something to the parameter of the function which is unknown, we don't know what the function does and what the function expects as the parameter. 但我不建议这样做,因为这基本上是在函数的参数中添加未知的内容,因此我们不知道函数的功能以及该函数作为参数的期望。 Doesn't sound very safe, does it? 听起来不是很安全吗?

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

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