最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

theme development - View list of all attachments on site

programmeradmin2浏览0评论

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 badges
Add a comment  | 

2 Answers 2

Reset to default 1

attachment.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/

发布评论

评论列表(0)

  1. 暂无评论