php - 如何在 WordPress 中显示来自自定义字段的多个图像
问题描述
我有一个名为“图像”的自定义字段。我想将此自定义字段显示为图片。我怎样才能做到这一点?
我写了一个 php 代码,但它只显示 1 张图像,它没有显示任何其他图像。
我的代码:
<?php $images = get_post_meta(get_the_ID(), 'images', true); ?>
<?php if ( $images && is_single() ): ?>
<?php
$images = get_post_meta( $post->ID, 'images' );
if ( $images ) {
foreach ( $images as $attachment_id ) {
$thumb = wp_get_attachment_image( $attachment_id, 'full' );
$full_size = wp_get_attachment_url($attachment_id);
printf( '<a href="%s">%s</a>', $full_size, $thumb );
}
}
?></br>
<?php endif; ?>
解决方案
这应该有效。解释见评论。
<?php
$images = get_post_meta(get_the_ID(), 'images', true);
if ( $images && is_single() ):
$images = get_post_meta( $post->ID, 'images' );
if ( $images ) {
//you can't loop through strings, you need to convert the string to an array
$images = explode( ",", $images );
foreach ( $images as $attachment_id ) {
$thumb = wp_get_attachment_image( intval($attachment_id), 'full' );
$full_size = wp_get_attachment_url($attachment_id);
printf( '<a href="%s">%s</a>', $full_size, $thumb );
}
}
echo '<br>';
endif;
?>
推荐阅读
- node.js - 在不使用 sequelize.sync 的情况下定义 sequelize 关联的正确方法是什么?
- d3.js - d3.js - 使用树形布局绘制文件目录
- selenium-webdriver - Capybara ,MSEdgeDriver : Selenium::WebDriver::Error::JavascriptError: javascript error: arguments[0].matches 不是函数
- java - 10000ms 后无法从 /192.168.0.102(端口 47108)连接到 /192.168.0.101(端口 443)
- python - 如何在没有日期和年份的情况下仅获取 python 中的时间?
- php - 将匿名函数保存到常量数组
- amazon-web-services - 来自 crontab 上的 shell 脚本的 aws 命令
- sql - SQL 适用于 Athena Engine v1 但不适用于 v2
- windows - 更新驱动程序以使用 VS2019 / WDK 22000 构建后,Windows MS 签名的过滤器驱动程序无法在 Win 7 x64 上加载
- c++ - 调用 Ninja.exe 以 code1 退出