【问题标题】:Create dynamic ids for wordpress gallerie shortcode from custom fields从自定义字段为 wordpress 画廊简码创建动态 ID
【发布时间】:2014-07-28 18:23:13
【问题描述】:

这是我的第一篇文章,很高兴阅读其他问题。我正在寻找一种解决方案,将 ID 从我使用 ACF 插件创建的自定义字段中插入到 Wordpress 库短代码中。

短代码如下所示:

<?php echo do_shortcode('[gallery type="carousel" ids="47,48,49,etc..."]'); ?>

我想用自定义字段提供的值替换特定的 ID(数字)。例如:

<?php echo get_field('galleryimage_1'); ?>
<?php echo get_field('galleryimage_2'); ?>

这会给我正确的第一个和第二个 ID,我只是不知道如何将它包含到短代码中。

问候

【问题讨论】:

    标签: php wordpress image-gallery custom-fields shortcode


    【解决方案1】:

    如果您只使用标准 ACF 自定义字段(而不是 Gallery 或 Repeater ACF 插件),我建议:

    <?php $limit = 10; //number of your galleryimage_n fields
    $imgArr = array();
    for($ctr = 1; $ctr <= $limit; $ctr++){
        $value = get_field('galleryimage_' . $ctr);
        if($value){
            $imgArr[] = $value;
        }
    }
    $imgIds = implode(",", $imgArr);
    echo do_shortcode('[gallery type="carousel" ids="'. $imgIds .'"]'); ?>
    

    【讨论】:

      【解决方案2】:

      看起来不错,比我的解决方案好得多:

      echo do_shortcode('[gallery type="carousel" ids="' . get_field('galleriebild_1') . ', "]');
      

      【讨论】:

        猜你喜欢
        • 2023-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-13
        • 2020-02-08
        • 1970-01-01
        相关资源
        最近更新 更多