• I know there’s code to print all posts on a website, and all pages, and you can of course print them with their featured image, but is there a way to print out, on a page (actually, preferably paginated into 500 per page), all images on a WordPress blog (my own)?

    • This topic was modified 4 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not a Developing with WordPress topic
Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    To get literally every image used on the site would be rather involved. However, getting all images in the media library is much easier. Every media library item is associated with the special post type “attachment”. These can be queried just like regular posts. If you have media besides images, the query can be qualified by a list of desired MIME types. With the queried list of attachment posts you can output links or images from the data associated with each attachment post.
    https://developer.www.ads-software.com/reference/classes/wp_query/

    Thread Starter ordresser

    (@ordresser)

    OK, it is the media library I’d like to list.

    This is the code I’ve been working with, which does display all posts. How can I modifty it to print every media file Image and Image Title? (linked to attachment file of course, or displaying the URL):

    
    
    <?php 
    // the query
    $wpb_all_query = new WP_Query(array('post_type'=>'post', 'post_status'=>'publish', 'posts_per_page'=>-1)); ?>
    
    <?php if ( $wpb_all_query->have_posts() ) : ?>
    
    <ul>
    
    	<!-- the loop -->
    	<?php while ( $wpb_all_query->have_posts() ) : $wpb_all_query->the_post(); ?>
    		<li><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?><?php the_title(); ?></a></li>
    	<?php endwhile; ?>
    	<!-- end of the loop -->
    
    </ul>
    
    	<?php wp_reset_postdata(); ?>
    • This reply was modified 4 years, 10 months ago by ordresser.
    • This reply was modified 4 years, 10 months ago by ordresser.
    Moderator bcworkz

    (@bcworkz)

    If you don’t need associated post data and only need the images, don’t query for “post” post type at all. Query for “attachment” post type instead. With each attachment ID returned by the query, use get_image_tag() to get the desired <img> tag for output.

    Thread Starter ordresser

    (@ordresser)

    I don’t need associated post data, just the images and their URLs.

    How do I edit this code to do what you mention, though? I’ve been trying to edit the code myself but beind novice at php I can’t get it to print anything at all.

    Moderator bcworkz

    (@bcworkz)

    Change the query:
    WP_Query(array('post_type'=>'attachment', 'post_status'=>'inherit', 'posts_per_page'=>-1))

    and the output line within the while loop:
    <li><?php echo get_image_tag( get_the_ID(), '', '', 'none', 'medium'); ?></li>
    (untested, I’m not totally sure how well that loop works when the post is not a real “post”. There’s a different loop we can use if there’s trouble)

    Thread Starter ordresser

    (@ordresser)

    Thanks! Your code works.

    With a couple additions, I added titles and made them all link.

    But is there a way to make this code paginate, so that only 50 will display on a page?

    Moderator bcworkz

    (@bcworkz)

    To start with, change the “posts_per_page” argument from -1 to whatever post count you want. The trouble comes in the pagination links on the page. The usual pagination functions placed on templates by themes don’t correctly handle custom queries. You will want to use paginate_links() passed appropriate arguments to show appropriate pagination links on the page. See
    https://developer.www.ads-software.com/reference/functions/paginate_links/

    Use the example code in the user notes near the bottom of the page as a guide for how to use the function. You’ll need to modify your WP_Query args so that it returns the correct posts for the page that was requested.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Way to display links/images to all images on website?’ is closed to new replies.