admin管理员组文章数量:1325441
I'm new to WordPress theme development and I'm trying to figure out if there is a way to display a list of public attachments similar to how you can view a list of posts with a specific tag or author.
That is, if I want to retrieve a list of posts with a specific tag, I can submit something like '?tag=mytag' in the URL and Wordpress will spit out all the posts tagged with 'mytag'. So what is the equivalent of this for viewing attachments? Or is there one?
What I'm specifically wanting to do is allow the user to click a link and have a gallery of attachments (images, video, music, etc) display. I want attachments from ALL posts on the site, not just attachments of one specific post.
I've looked into the attachment.php file, but this seems to only be to display the details of a specific attachment with a given attachment_id value (ie: '?attachment_id=246').
Can someone steer me in the right direction?
I'm new to WordPress theme development and I'm trying to figure out if there is a way to display a list of public attachments similar to how you can view a list of posts with a specific tag or author.
That is, if I want to retrieve a list of posts with a specific tag, I can submit something like '?tag=mytag' in the URL and Wordpress will spit out all the posts tagged with 'mytag'. So what is the equivalent of this for viewing attachments? Or is there one?
What I'm specifically wanting to do is allow the user to click a link and have a gallery of attachments (images, video, music, etc) display. I want attachments from ALL posts on the site, not just attachments of one specific post.
I've looked into the attachment.php file, but this seems to only be to display the details of a specific attachment with a given attachment_id value (ie: '?attachment_id=246').
Can someone steer me in the right direction?
Share Improve this question asked Jun 19, 2013 at 5:34 skubikskubik 111 silver badge2 bronze badges2 Answers
Reset to default 1attachment.php
file displays only one attachment (like single.php
or page.php
).
There is no way to list all attachments without coding. WordPress doesn't have such list ready to use.
But it's rather easy to do it by yourself.
Add your custom page template. Assign it to some page. And place something like this inside this template:
$args = array(
'post_type'=>'attachment', // you want to show attachments
'posts_per_page'=>-1, // all of them
// other params
);
$attachments = new WP_Query( $args );
while ( $attachments->have_posts() ): $attachments->the_post();
//display attachment - current attachment is placed in global variable $post
// you can also use template tags
endwhile;
The other solution here does not seem to work on admin screen. This worked for me:
$args = array('post_type'=>'attachment','numberposts'=>null,'post_status'=>null);
$attachments = get_posts($args);
if($attachments){
foreach($attachments as $attachment){
// here your code
print_r($attachment);
}
}
Source: http://www.kvcodes/2015/12/get-attachments-wordpress/
本文标签: theme developmentView list of all attachments on site
版权声明:本文标题:theme development - View list of all attachments on site 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742119488a2421629.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论