如何在WordPress中使用ACF将多个图像添加到自定义页面模板中?

发布于 01-22 00:37 字数 325 浏览 3 评论 0原文

我有许多页面(400),它们都在视觉编辑器(Divi)中单独构建,尽管它们除了每个页面上的图像以外都是相同的 - 这是我的第一个网站,所以当我知道时,我不知道这种方法。我开始了。

它们都是pagex的儿童页面,他们的slug始于一个通用词。例如:

mysite.com/pagex/commonword-page1/

mysite.com/pagex/commonword-page2/

如果我想使用自定义页面模板并插入每个页面的唯一图像,我该怎么做?每个页面添加一个高级自定义字段,包含图像ID/URL数组是一个不错的选择吗?然后只需调用这个并循环遍历每个循环并插入DOM?

I have a number of pages (400) which are all build individually in a visual editor (DIVI), even though they are identical other than the images on each page - This was my first website and so I knew no better than this approach when I began.

They are all child-pages of pageX and their slug begins with a common word. eg:

mysite.com/pageX/commonword-page1/

mysite.com/pageX/commonword-page2/

If I wanted to use a custom page template and insert unique images for each page, how would I go about doing this? Would adding an advanced custom field for each page containing an array of image ids/urls be a good option? Then simply call this and loop through each one and insert into the DOM?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最美的太阳2025-01-29 00:37:16

您将从插件目录下载并安装ACF。然后,创建一个自定义画廊字段。

在页面上,使用自定义字段,在图库中选择所需的图像。然后在模板中使用此代码来创建WordPress图库:

// Load value (array of ids).
$image_ids = get_field('gallery'); // slug name of the field you created
if( $image_ids ) {

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

    // Generate and do shortcode.
    // Note: The following string is split to simply prevent our own website from rendering the gallery shortcode.
    $shortcode = sprintf( '[' . 'gallery ids="%s"]', esc_attr($images_string) );
    echo do_shortcode( $shortcode );
}

从Gallery Fields的ACF文档中提取。

这是他们的链接:

You would download and install ACF from the Plugins directory. Then, create a custom gallery field.

On the page, using the custom field, select the images you want in the gallery. Then in the template, to create a WordPress Gallery use this code:

// Load value (array of ids).
$image_ids = get_field('gallery'); // slug name of the field you created
if( $image_ids ) {

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

    // Generate and do shortcode.
    // Note: The following string is split to simply prevent our own website from rendering the gallery shortcode.
    $shortcode = sprintf( '[' . 'gallery ids="%s"]', esc_attr($images_string) );
    echo do_shortcode( $shortcode );
}

This was pulled from the ACF documentation for gallery fields.

Here is their link:
https://www.advancedcustomfields.com/resources/gallery/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文