如何获取我在WordPress中上传的图像的标题,名称和描述?

时间:2012-11-26 17:38:47

标签: php wordpress

我写了这个查询:

$album_id = get_the_id();
$photos = $wpdb->get_results(
    "select * from wp_postmeta where post_id = '"
    . $album_id
    . "' AND meta_key = 'gallery_ph' order by meta_id desc"
);

现在,我想获取所有缩略图,其中包含此图片的标题,名称和说明。

我在DB中找到了:

meta_id | post_id | meta_key   | meta_value
577     | 346     | gallery_ph | http://url to the image

和缩略图

meta_id | post_id | meta_key                 | meta_value
569     | 348     | _wp_attachment_image_alt | Jury Datiop

这两个数据是如何连接的?我知道Jury Datiop有网址http://xxx,因为我可以登录wp-admin。但是如何编写选择查询?如何连接图像并获取标题,名称,描述?

1 个答案:

答案 0 :(得分:1)

您无需直接查询数据库。

WordPress有很多自定义函数可供您检索所需的数据。

检查:

WordPress Codex : Function Reference

特别:


将数据库查询转换为WordPress函数:

未经测试的代码,使用Debug检查每项操作的结果

$album_id = get_the_id();

$photos = get_children( array(
    'post_parent'    => $album_id, 
    'post_type'      => 'attachment', 
    'meta_key'       => 'gallery_ph', 
    'post_mime_type' => 'image', 
    'order'          => 'DESC', 
    'orderby'        => 'meta_id') 
);

// DEBUG
// echo '<pre>' . print_r( $meta, true ) . '</pre>';

if( $photos )
{
    foreach( $photos as $img )
    {
        // DEBUG
        // echo '<pre>' . print_r( $img, true ) . '</pre>';

        $meta = wp_get_attachment_metadata( $img->ID );

        // DEBUG
        // echo '<pre>' . print_r( $meta, true ) . '</pre>';
    }
}