Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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
Php 如何在变量中传递参数_Php_Wordpress_Advanced Custom Fields - Fatal编程技术网

Php 如何在变量中传递参数

Php 如何在变量中传递参数,php,wordpress,advanced-custom-fields,Php,Wordpress,Advanced Custom Fields,我在WordPress主题中使用ACF <div class="hero"> <?php $landscape_image_1x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-1x' ); $landscape_image_2x = wp_get_attachment_image_url( get_field('

我在WordPress主题中使用ACF

<div class="hero">
    <?php
        $landscape_image_1x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-1x' );
        $landscape_image_2x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-2x' );
        $portrait_image_1x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-1x' );
        $portrait_image_2x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-2x' );
        $image_alt = get_post_meta( $hero_landscape, '_wp_attachment_image_alt', true);
    ?>
    <picture>
        <source
            media="(orientation: landscape)" 
            srcset="
                <?= $landscape_image_1x; ?> 1x,
                <?= $landscape_image_2x; ?> 2x"
        >
        <source
            media="(orientation: portrait)" 
            srcset="
                <?= $portrait_image_1x; ?> 1x,
                <?= $portrait_image_2x; ?> 2x"
        >
        <img src="<?= $landscape_image; ?>" alt="<?= $image_alt; ?>">
    </picture>
</div>

如果我没弄错的话,你对我的长篇大论感到恼火

 <?php
        $landscape_image_1x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-1x' );
        $landscape_image_2x = wp_get_attachment_image_url( get_field('landscape_image'), 'hero-landscape-2x' );
        $portrait_image_1x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-1x' );
        $portrait_image_2x = wp_get_attachment_image_url( get_field('portrait_image'), 'hero-portrait-2x' );
        $image_alt = get_post_meta( $hero_landscape, '_wp_attachment_image_alt', true);
    ?>
但这并没有多大帮助

另一个想法是用函数封装获取url。请注意,您可以消除一次性使用变量

function image_url($orientation, $location) {
    return wp_get_attachment_image_url( get_field($orientation), $location );
}
?>

    <picture>
        <source
            media="(orientation: landscape)" 
            srcset="<?= image_url('landscape_image', 'hero-landscape-1x' ?>" 1x,
                <?= image_url('landscape_image','landscape_image_2x') ?> 2x"
        >
函数图像\u url($orientation,$location){
返回wp_get_attachment_image_url(get_字段($orientation),$location);
}
?>
2x“
>
它仍然有点冗长,但是您已经消除了不必要的赋值,同时在视图中保持描述性

如果您担心与现有函数名冲突,也可以将其关闭:

$url = function ($orientation, $location) {
    return wp_get_attachment_image_url( get_field($orientation), $location );
}
?>
   <picture>
        <source
            media="(orientation: landscape)" 
            srcset="<?= $url('landscape_image', 'hero-landscape-1x' ?>" 1x,
                <?= $url('landscape_image','landscape_image_2x') ?> 2x"
        >
$url=函数($orientation,$location){
返回wp_get_attachment_image_url(get_字段($orientation),$location);
}
?>
2x“
>

对不起,我不知道你在这里的意思。你到底想解决什么问题?发布的代码(第一个代码块)有效吗?你是对的,第二个代码块是非常错误的(因为该函数返回一个字符串或false,不能用作函数),并且它没有给我们任何提示,告诉我们你实际上在做什么。第一个代码块工作得非常好。目前,我正在重复很多代码。每个变量所改变的只是字段名,例如
scape\u image
,以及大小参数,例如
hero scape
。我想知道是否可以在PHP中编写一个变量,然后在模板中传递每个变量的字段名和大小变量。这更有意义吗?
function image_url($orientation, $location) {
    return wp_get_attachment_image_url( get_field($orientation), $location );
}
?>

    <picture>
        <source
            media="(orientation: landscape)" 
            srcset="<?= image_url('landscape_image', 'hero-landscape-1x' ?>" 1x,
                <?= image_url('landscape_image','landscape_image_2x') ?> 2x"
        >
$url = function ($orientation, $location) {
    return wp_get_attachment_image_url( get_field($orientation), $location );
}
?>
   <picture>
        <source
            media="(orientation: landscape)" 
            srcset="<?= $url('landscape_image', 'hero-landscape-1x' ?>" 1x,
                <?= $url('landscape_image','landscape_image_2x') ?> 2x"
        >