代码之家  ›  专栏  ›  技术社区  ›  superUntitled

Wordpress:查询posts媒体库中的所有图像

  •  8
  • superUntitled  · 技术社区  · 15 年前

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

    我认为下一个图片链接()/上一个图片链接();模板标签和我发现的一样接近。

    我认为这应该很接近:

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

    2 回复  |  直到 15 年前
        1
  •  12
  •   ariefbayu    13 年前

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

        2
  •  0
  •   Nagendra Rao ivaneliasoo    11 年前

    如果您正在寻找一个插件来管理图像库,您可以使用 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; ?>