通过get_children
和wp_get_attachment_image
,我设法将3个想要的画廊图像从特定帖子中拉到首页。
我正在努力工作以获取the_permalink
,以便在您单击图像时将其带到帖子本身。我可以通过获取图像的缩略图轻松地做到这一点,但是我需要完整尺寸的图像。
<?php $args = array(
'posts_per_page' => 1,
'category_name' => 'lifestyle'
);
$q = new WP_Query( $args);
if ( $q->have_posts() ) {
while ( $q->have_posts() ) {
$q->the_post();
the_title();
}
wp_reset_postdata();
} ?>
<?php
$images =& get_children( array (
'post_parent' => $post->ID,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => 3,
));
if ( empty($images) ) {
} else {
foreach ( $images as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, '' );
}
}
?>