我使用以下代码将所有附件分配给我的自定义帖子类型 ..但现在我想在循环中获取帖子ID ..我怎样才能做到这一点??
$query = new WP_Query(
array(
'post_type' => 'portfolio', // adjust your custom post type name here
'posts_per_page' => -1,
'fields' => 'ids'
)
);
$image_query = new WP_Query(
array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'post_parent__in' => $query->posts,
'order' => 'DESC'
)
);
if( $image_query->have_posts() ){
while( $image_query->have_posts() ) {
$image_query->the_post();
$imgurl = wp_get_attachment_url( get_the_ID() );
if(!empty($output)) $output = 'Sorry, no attachments found.';
$output .= '<a href="'.$imgurl.'"><img src="'.$imgurl.'"></a>';
echo $output;
}
谢谢!
答案 0 :(得分:1)
根据您在循环中查找的ID,您可以执行以下操作
$post-ID
- &gt;从正在显示的附件帖子中返回帖子ID
$post->post_parent
- &gt;返回父帖子ID