9

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

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

我认为 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';

谢谢。

4

2 回答 2

12

在 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 />";
}

算法是这样的。

于 2009-09-02T20:24:34.430 回答
0

如果您正在寻找管理图片库的插件,您可以使用attachments插件,

http://wordpress.org/plugins/attachments/

它使图库保持独立,并且不会将图库短代码放在帖子内容中,从而为您提供完全控制您的帖子/页面/自定义帖子中的图像显示。您还可以通过拖放来更改图像的顺序

这是如何检索您的画廊图像的示例代码,

<?php $attachments = new Attachments( 'attachments' ); /* pass the instance name */ ?>
<?php if( $attachments->exist() ) : ?>
  <h3>Attachments</h3>
  <p>Total Attachments: <?php echo $attachments->total(); ?></p>
  <ul>
    <?php while( $attachments->get() ) : ?>
      <li>
        ID: <?php echo $attachments->id(); ?><br />
        Type: <?php echo $attachments->type(); ?><br />
        Subtype: <?php echo $attachments->subtype(); ?><br />
        URL: <?php echo $attachments->url(); ?><br />
        Image: <?php echo $attachments->image( 'thumbnail' ); ?><br />
        Source: <?php echo $attachments->src( 'full' ); ?><br />
        Size: <?php echo $attachments->filesize(); ?><br />
        Title Field: <?php echo $attachments->field( 'title' ); ?><br />
        Caption Field: <?php echo $attachments->field( 'caption' ); ?>
      </li>
    <?php endwhile; ?>
  </ul>
<?php endif; ?> 
于 2013-07-01T11:44:03.977 回答