使用slug“Gorsoph”从类别中获取10篇帖子,在循环中获取帖子子项,按mime类型筛选以获取适当的文件,添加到列表中
/**
* get 10 post from category with SLUG gospel
*/
$args = array(
\'category_name\' => \'gospel\',
\'posts_per_page\' => 10
);
$songs = array();
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
/**
* get post children, filter by post_mime_type, limit 1
*/
$args = array(
\'post_parent\' => get_the_ID(),
\'post_type\' => \'attachment\',
\'posts_per_page\' => 1,
\'post_status\' => \'any\',
\'post_mime_type\' => \'audio\'
);
$attachments = get_children( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
$songs[] = wp_get_attachment_url( $attachment->ID);
}
}
}
}
/**
* print songs
*/
print_r($songs);
/**
* reset postdata
*/
wp_reset_postdata();