Php Wordpress-循环浏览媒体库中的图像,其中标题包含文本“carousel”

Php Wordpress-循环浏览媒体库中的图像,其中标题包含文本“carousel”,php,wordpress,Php,Wordpress,Wordpress是一个新手,我想做的是在PHP中创建一个循环,通过管理界面将图像上传到媒体库 我只需要显示标题文本(上传期间完成的字段)包含文本转盘的图像。我想得到图像SRC和描述 这可能吗?因为现在我知道至少有一种方法可以进行WP_查询或获取_posts,根据post_摘录值筛选出帖子,而post_摘录值就是存储标题的位置,所以您必须添加一个过滤器来修改WordPress创建的SQL查询 这应该会让你感觉很好: // Our filter function function my_filte

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

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


这可能吗?

因为现在我知道至少有一种方法可以进行WP_查询或获取_posts,根据post_摘录值筛选出帖子,而post_摘录值就是存储标题的位置,所以您必须添加一个过滤器来修改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;
}
我建议:使用get_posts时,需要将suppress_filters键设置为false。如果您决定改用WP_查询,则可以安全地删除。