Wordpress - 循环播放媒体库中的图像,其中标题包含文本'carousel'

时间:2012-02-21 16:01:15

标签: php wordpress

对Wordpress来说很新,我想做的是在PHP中创建一个循环,通过管理界面将图像上传到媒体库。

我只需要显示图片,其中标题文本(上传期间完成的字段)包含文本轮播。我想得到图像SRC和描述。

这可能吗?

1 个答案:

答案 0 :(得分:0)

因为现在有办法(我至少知道)让WP_Queryget_posts根据他们的post_excerpt值来过滤帖子,这是存储标题的位置,您将不得不添加一个过滤器来修改WordPress创建的SQL查询。

这应该是你很远的地方:

// Our filter function
function my_filter($where) {

    $where .= " AND post_excerpt = 'carousel'";

    return $where;
}

// Add the filter
add_filter( 'posts_where' , 'my_filter' );

// get the posts (i.e. your images)
$images = get_posts(array(
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'posts_per_page' => -1,
    'suppress_filters' => false,
));

// Remove the filter once we're done
remove_filter( 'posts_where' , 'my_filter' );

// Loop through images
foreach ($images as  $image) {
    $img_data = wp_get_attachment_image_src($image->ID);

    $src = $img_data[0];
    $description = $image->post_content;
}