WordPress:查询帖子媒体库中的所有图像

标签 wordpress wordpress-theming

你好!我正在寻找一种列出帖子媒体库中所有图像文件的方法。

我的意思是,如果在创建或编辑帖子时上载了文件,是以某种方式与该帖子相关联的文件,我是否可以从该数据中创建一个列表。

我认为next_image_link()/previous_image_link();模板标签与我所发现的非常接近。

我认为这应该很接近:

$query = 'SELECT * FROM `wp_posts` 
WHERE `post_parent` = \''.$_GET['post_id'].'\' 
AND  `post_mime_type` = \'image/jpeg\' 
ORDER BY `menu_order` ASC';

谢谢。

最佳答案

用wordpress术语来说,您上传到特定帖子的每个图像都称为附件。
要列出所有附件,可以使用get_children()函数:

$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=10' );

$counter=0;
foreach( (array) $images as $attachment_id => $attachment )
{
   $counter++;
   echo "<a href='".wp_get_attachment_link( $attachment_id ) . "'>image $counter</a><br />";
}

该算法就是这样。

关于WordPress:查询帖子媒体库中的所有图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1369833/

相关文章:

php - Wordpress add_rewrite_rule 不返回 $matches[] 数组项

php - wordpress前后内容添加div

html - 谷歌显示 "A description for this result is not available because of this site' s robots.txt“

html - 无法删除 h1 元素

php - 请帮助我理解这个 php 调试错误(Braintree-WooCommerce 扩展)

php - 组合框不会显示来自 MySQL 的值

php - 将自定义 URL 添加到 WordPress header

html - 如何从wordpress中删除页脚 "powered by wordpress"

wordpress - 修改 WordPress WooCommerce 面包屑

php - 在 Wordpress 中执行复制器备份时构建中断