如何在变量中传递参数

How to pass a parameter in a variable

我在 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>

第一个代码块完美运行。

目前,我正在重复很多代码。每个变量的所有变化都是字段名称,例如landscape_image 和大小参数,例如hero-landscape-1x.

在 PHP 中如何在回显变量时传递值?这是我非常错误的解释我的意思的方式。

<?php
 $image = wp_get_attachment_image_url( get_field( $field ), $parameter );
 echo $image('landscape_image', 'hero_landscape_1x');
 echo $image('landscape_image', 'hero_landscape_2x');
<?

如果我没理解错的话,你对

的冗长感到恼火
 <?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);
    ?>

有几种方法可以选择;我看到 get_field() 的重复,但比 wp_get_attachment_image_url(get_field())

的重复更多

TBH,除了冗长的函数名称之外,这没什么大不了的,除非你发现自己在重复这个块。

所以一种选择是分配重复的值,例如

$landscape = get_field('landscape_image');
$portrait  = get_field('portrait_image');

但这并没有多大帮助。

另一个想法是用函数封装获取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"
        >

它仍然有点冗长,但您已经消除了不必要的赋值,同时在视图中保留了描述性。

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

$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"
        >