以下是php文档的一部分,其中包含一些条件。第一个(如果是事实的话)将匿名函数存储在一个变量中$the_image_styles
,该变量在以后成功调用了几行。第二个(如果是事实的话)将匿名函数存储在一个变量中$the_overlay_styles
,该变量永远不会随后执行。
WordPress输出以下内容:调用未定义的函数the_overlay_styles()
在我看来,这两个声明/调用之间没有区别,但显然并非如此。我想念什么?
<?php
$banner = get_field('banner_image');
// Check for image
if ($banner) :
$xpos = get_field('banner_xpos');
$ypos = get_field('banner_ypos');
// Style string for background image
$the_image_styles = function() use ($banner, $xpos, $ypos){
echo "background-image:url({$banner['sizes']['large']}); background-position:{$xpos} {$ypos}";
};
// Check for overlay, for later use
$has_overlay = get_field('has_banner_overlay');
?>
<!-- Article header with banner -->
<header style="<?php $the_image_styles(); ?>"> <<-- This function call works
<?php
if ($has_overlay) :
$banner_colour = get_field('banner_colour');
$banner_opacity = number_format( get_field('banner_opacity') / 100, 2 );
// Style string for imageoverlay
$the_overlay_styles = function() use ($banner_colour, $banner_opacity){
echo "background:{$banner_colour}; opacity:{$banner_opacity}";
};
?>
<div style="<?php the_overlay_styles(); ?>"></div> <<-- This call never executes
<?php endif; ?>
<h1><?php the_title(); ?></h1>
</header>
<?php else: ?>
您正在定义一个$the_overlay_styles variable
,它引用一个闭包。打电话给你,你会做的$the_overlay_styles();
。
但是不要。只需将其定义为正常功能即可:
function the_overlay_styles() {
您知道什么,甚至不要这样做。只需在这里创建一个变量!少得多的混乱。
$the_overlay_styles = "background: $banner_colour; opacity: $banner_opacity";
现在,您可以echo $the_overlay_styles
在需要时使用它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句