通过id从单页获取附件,并将其分页。 (WordPress的)
How to get attachment from single page by ID, and set 12 img per page?
'post_parent' => 145, // not working, but without this param I get all attachments
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$attachment = new WP_Query( array(
'post_parent' => 145, // not working, but without this param I get all attachments
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',
'posts_per_page' => 12,
'paged' => $paged
) );
if ( $attachment->have_posts() ) :
while ( $attachment->have_posts() ) : $attachment->the_post();
?>
<div class="gallery__item"><a href="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" data-fancybox><img src="<?php echo wp_get_attachment_url( $attachment->ID , false ); ?>" alt=""></a></div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
如何按ID从单页获取附件,并在每页设置12 img? p> \ n
'post_parent'=&gt; 145,//没有工作,但没有这个参数我得到所有附件 p>
&lt;?php
$ paged =(get_query_var('paged'))? get_query_var('paged'):1;
$ attachment = new WP_Query(array(
'post_parent'=&gt; 145,//不工作,但没有这个参数我得到所有附件
'post_status'=&gt; 'inherit',
'post_type'=&gt;'attachment',
'post_mime_type'=&gt;'image / jpeg,image / gif,image / jpg,image / png',
'posrs_per_page'=&gt; 12,
'paged'=&gt; $ paged
));
if($ attachment-&gt; have_posts()):
while($ attachment-&gt; have_posts()):$ attachment-&gt; the_post();
?&gt;
&lt; div class =“gallery__item”&gt;&lt; a href =“&lt;?php echo wp_get_attachment_url($ attachment-&gt; ID,false);?&gt;” data-fancybox&gt;&lt; img src =“&lt;?php echo wp_get_attachment_url($ attachment-&gt; ID,false);?&gt;” alt =“”&gt;&lt; / a&gt;&lt; / div&gt;
&lt;?php
endwhile;
wp_reset_postdata();
endif;
?&gt;
code> pre>
div>
I found out what the problem was. I use post_parent and tells post ID, thinking that take all attachments from this post, but WP_Query returns me ID's of images.
I get all id's of images in array with get_post_gallery()
$gal = get_post_gallery(11,false);
and explode it
$ids = explode(',', $gal['ids']);
after then everything worked!
also I use post__in insted post_parent
The final code:
<?php
$gal = get_post_gallery(11,false);
$ids = explode(',', $gal['ids']);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$attachment = new WP_Query( array(
'post__in' => $ids,
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image/jpeg,image/gif,image/jpg,image/png',
'posts_per_page' => 12,
'paged' => $paged
) );
if ($attachment -> have_posts() ) :
while ($attachment -> have_posts() ) : $attachment -> the_post();
?>
<div class="gallery__item"><a href="<?php echo wp_get_attachment_url(); ?>" data-fancybox><img src="<?php echo wp_get_attachment_url(); ?>" alt=""></a></div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
By looking to the code, you are using post_parent which is quiet clearly explain in
which basically says that only the post that has child will be return of the currently provided post id i.e. 145.
I think you have to use post_parent__in than the current one.
Hope this works for you.
Thank You