wordpress - 如何用逗号列出 wordpress 附件 URL?
问题描述
我的帖子上有一个图片库字段,我可以在其中添加多张图片。
我需要在里面用逗号列出附件 urldata-thumb"url1,url2,url3"
如何在下面的函数中用逗号分隔拇指?
function multi_thumbs_array(){
global $post;
$images = get_post_meta( $post->ID, 'images', true );
if( $images ):
$size = 'thumbnail';
foreach( $images as $image ):
echo wp_get_attachment_image_url($image, $size);
endforeach;
endif;
}
也试过这个:
function multi_thumbs_array(){
global $post;
$images = get_post_meta( $post->ID, 'images', true );
if( $images ) {
$size = 'thumbnail';
foreach( $images as $image ) {
$thumbs = wp_get_attachment_image_url($image, $size);
}
}
if( is_array($thumbs) ){
return implode(',', $thumbs);
}
return false;
}
上面的代码可以正常工作,但它的列表 url 没有任何逗号。我尝试multi_thumbs_array()
在 implode 中使用,但是当我这样做时它不起作用。谢谢!
解决方案
您需要稍微调整一下代码。Fe $thumbs 应该是一个数组,而不是一个字符串。
function multi_thumbs_array(){
global $post;
$thumbs=array();
$images = get_post_meta( $post->ID, 'images', true );
if( $images ) {
$size = 'thumbnail';
foreach( $images as $image ) {
$thumbs[] = wp_get_attachment_image_url($image, $size);
}
} else {
$get_first_image=get_attached_media( 'image', $post->ID );
$get_first_image=array_shift( array_values($get_first_image) );
$thumbs_arr = wp_get_attachment_image_src( $get_first_image->ID );
$thumbs[] = $thumbs_arr[0];
}
/* no need to check if $thumbs is array as it is
declared in array and it's type doesn't get modified in the code */
return implode(',', $thumbs); //returns empty string if $thumbs is empty
}
推荐阅读
- javascript - 使用 JS 更改 PHP 包含在 HTML 页面中
- python - KeyError:'password1':password1 = self.cleaned_data['password1']
- html - CSS 网格布局:用更少的输入指定网格位置?
- python - 每次机器人运行时,如何更新函数的特定参数?
- git - Git 中提交和合并的概念(适用于了解 SVN 的人)
- python - 如何修复没有显示名称和 $DISPLAY 环境变量 python 错误
- slack - 单击选择菜单会在松弛模式视图中打开另一个菜单
- netbeans - 有没有办法使用快捷方式或 Netbeans 中的快捷方式插件在拆分的窗口之间移动?
- macos - Applescript 让 iTunes 通过 Apple Music URL 播放专辑?
- python - 使用列表比较长度不等的字符串