【发布时间】:2017-08-15 09:57:37
【问题描述】:
我的问题:我在一篇文章中加载了几张图片,因此它们被动态分配给该文章的附件。因此,当我从帖子中删除图像时,它们仍会根据我调用的项目数量反映在画廊滚动中。
这是我调用附件的代码
function sunset_get_attachment($num = 1)
{
$output = '';
if (has_post_thumbnail() && $num == 1):
$output = wp_get_attachment_url(get_post_thumbnail_id(get_the_ID())); else:
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => $num,
'post_parent' => get_the_ID(),
));
if ($attachments && $num == 1):
foreach ($attachments as $attachment):
$output = wp_get_attachment_url($attachment->ID);
endforeach; elseif ($attachments && $num > 1):
$output = $attachments;
endif;
wp_reset_postdata();
endif;
return $output;
}
WordPress 说 您可以在 2 个地方上传图像:媒体页面 (uploads.php) 或从帖子/页面编辑页面中的添加媒体按钮。当您从帖子编辑屏幕上传图像时,所有上传图像的“post_parent”都设置为当前帖子。当您从媒体页面上传图片时,'post_parent' 设置为 0。
当您在媒体页面上上传的图片首次插入帖子内容或包含在帖子内的图库中时,“post_parent”字段将更改为当前帖子,就像使用添加帖子编辑屏幕的媒体按钮。
您注意到,特色图片不会发生这种情况,当上传到媒体页面的图片用作帖子的特色图片时,其 post_parent 保持不变。
唯一的变化是为帖子创建了一个元字段“_thumbnail_id”,并将其值设置为附件帖子的 id。
在此处输入图片说明
所以如果你的帖子 id 是 10,get_post_meta(10, '_thumbnail_id', true);将返回帖子缩略图的 ID。
请注意,将 post_parent 设置为帖子 id 的图像并不意味着它已插入帖子的内容中,它可以使用帖子添加媒体按钮上传,但绝不会插入帖子内容中。
这个国家也是如此,插入到帖子内容中的图像可以将 post_parent 设置为另一个帖子 id。
恳求 有没有办法只调用当前发布的帖子编辑器中的图像?
【问题讨论】:
标签: wordpress