【问题标题】:Create WordPress gallery shortcode from Advanced Custom Fields从高级自定义字段创建 WordPress 图库短代码
【发布时间】:2019-10-14 04:05:43
【问题描述】:

我按照 ACF 库页面 https://www.advancedcustomfields.com/resources/gallery/ 中的说明使用简码创建原生 Wordpress 库并从 ACF 库字段中的数据动态填充项目 ID。

示例: 编辑:我稍微修改了代码以包含$image_ids的定义

<?php

// Load value (array of ids).
$images = get_field('product_images');
$image_ids = get_field('product_images', false, false);

if( $images ) {

    // Generate string of ids ("123,456,789").
    $images_string = implode( ',', $image_ids );

    // Generate and do shortcode.
    $shortcode = sprintf( '', $images_string );
    echo do_shortcode( $shortcode );
}

现在我的问题是返回一个错误,指出 $image_ids 的值未定义,因此根本看不到定义的位置。

我有一些以前使用的旧代码:

<?php
// different product sizes (gallery)
if(get_field('product_images')) : ?>
    <h3>Product Images</h3>
    <?php 
    $image_ids = get_field('product_images', false, false);
    $shortcode = '[' . 'gallery ids="' . implode(',', $image_ids) . '"]';
    echo do_shortcode( $shortcode );
endif;
?> 

此代码确实有效,但返回错误php notice array to string conversion in jetpack-carousel。是的,我正在使用“Tiled Galleries Carousel without Jetpack”插件。

https://wordpress.org/support/topic/php-notice-array-to-string-conversion-in-jetpack-carousel/page/2/

我真的很想遵循 ACF 文档中推荐的方法,但它对我不起作用。我做错了什么?

【问题讨论】:

  • 我现在可以看到 $image_ids 是从 $image_ids = get_field('product_images', false, false); 派生的。错误已消失,但未显示任何图片库。

标签: php wordpress advanced-custom-fields


【解决方案1】:

我没有安装 ACF,但在阅读了他们的手册后,我发现您的代码存在一些问题。

问题一:您同时使用了 $images 和 $images_ids。最终,他们都在做同样的工作,那就是为你取回 ids 数组。它们的数据可能与最后两个参数不同。

尽管如此,当您检查是否有某些内容时,您会使用 $images 进行检查和评估,但随后您将使用 $images_ids 作为您的 short_code 字符串组合。对于这个错误,我认为这是因为没有可选参数的版本返回了一个值,而使用可选参数的 one($images_ids) 没有返回值,而是被 if 子句使用。

我认为您必须弄清楚要使用哪个 get_field 版本。一个有 3 个参数或一个有 1 个参数。

这是在我注意到某些内容后编辑的,为什么您没有得到任何东西,您可能需要联系 ACF 以更新他们的手册。 sprintf() 语句不会做任何事情。它只会生成一个空白字符串。您可能需要像下面这样更改 sprintf() 以遵循您的旧语句,如果它仍然显示数组到字符串的转换错误,则需要对其进行调试。

$image_ids = get_field('product_images', false, false);

if( $image_ids ) {

    // Generate string of ids ("123,456,789").
    $images_string = implode( ',', $image_ids );

    // Generate and do shortcode.
    $shortcode = sprintf( '[gallery ids="%s"]', $images_string );
    echo do_shortcode( $shortcode );
}

【讨论】:

  • 是的,两个变量都在声明相同的东西,有/没有可选参数。你的建议很有用。至于字符串转换错误——我相信这与“Tiled Gallery without Jetpack”插件直接相关
  • @RyanCoolwebs - 我很高兴我的建议很有用。谢谢。
  • 如果它可以帮助其他人——我对此感到困惑——短代码只是 [gallery]。
【解决方案2】:

据我尝试,ACF 网站 (https://www.advancedcustomfields.com/resources/gallery/) 上的示例不正确。这是一个对我有用的不太优雅的方法。

<?php 

// Get the Gallery Array created by ACF
$galleryarray = get_sub_field('gallery');

// Create an empty array to input the ID's in to
$galleryids = array();

// Loop through the Gallery array
foreach ( $galleryarray as $image ) {
    // Grab the image ID
    $imageid = $image['id'];
    // Put the image ID in to the empty array
    array_push($galleryids, $imageid);
}

// Convert the new array into a string, values separated by a comma
$images = implode(',', $galleryids);

// Run the default WordPress Gallery shortcode
echo do_shortcode( '[gallery ids="'.$images.'"]' );

?>

【讨论】:

    猜你喜欢
    • 2012-09-02
    • 2016-01-09
    • 2017-04-20
    • 2017-08-28
    • 2012-08-10
    • 2015-01-21
    • 2017-05-01
    • 1970-01-01
    • 2013-10-06
    相关资源
    最近更新 更多