php ACF库自定义短代码

gfttwv5a  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(139)

你好,我想为ACF画廊创建自定义短代码。我试图在我的孩子主题的functions.php中添加下面的代码。但我得到WordPress的严重错误。
https://gist.github.com/mwangepatrick/01cbf679c3c9e6256c8483c337c274d3

<?php

add_shortcode('display_acf_gallery', 'acf_gallery');

function acf_gallery ( $atts ) {

  if ( empty ( $atts['name'] ) ) return "";

    $images = get_field( $atts['name'] );

    if( $images ): ?>

    <?php foreach( $images as $image ): ?>

       <div class="thumbnail" style="float:left;" ><img src="<?php echo $image['sizes']['thumbnail']; ?>" alt="<?php echo $image['alt']; ?>" /></div>

    <?php endforeach; ?>

  <?php endif;

}
fumotvh3

fumotvh31#

Every ACF gallery field has its own return type setting (with a several options), you need to check this setting before working with the get_field() response in code. Visit your ACF group and expand the field settings to see the return type format for your field, as the code will be different for an array return option or an ID return option.
You can also use an ACF Views plugin to display any ACF field (including gallery) using a shortcode, without coding at all. You just need to create a View there and select fields to display, then the plugin generates a shortcode for you automatically, you can use the shortcode anywhere, just add it on any page and the ACF Views plugin will automatically generate markup for the selected fields and display them.
P.S. Here is a link to their official website - ACF Views: Display ACF fields and Posts . More information along with links to their YouTube tutorials and Docs can be found there.

相关问题