Gokul P P
Gokul P P

Reputation: 962

Fetching all images of a posts in wordpress

I need to fetch the featured images of posts in WordPress Currently, I am having the code for listing posts with images and text.

I am having a posts page that lists all posts I need to list the photos of the posts to another page

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>     
  <?php if ( has_post_thumbnail() ) : ?>
  <div class="entry-thumb">
    <?php the_post_thumbnail( 'oblique-entry-thumb'); ?>
    <a class="thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><i class="fa fa-link"></i></a> 
  </div>
  <?php else : ?>
  <div class="entry-thumb">       
    <img src="http://placehold.it/500x500" />
    <a class="thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><i class="fa fa-link"></i></a> 
  </div>    
  <?php endif; ?>    
  <?php //if ( has_post_thumbnail() ) : ?>
  <div class="post-inner post-inner-height">
    <?php //else : ?>
    <!-- <div class="post-inner no-thumb"> -->
    <?php //endif; ?>    
    <header class="entry-header">
      <?php the_title( sprintf( '<h1 class="entry-title entry-title-height"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h1>' ); ?>

      <?php if ( 'post'==g et_post_type() && !get_theme_mod( 'meta_index') ) : ?>         
      <!-- .entry-meta -->
      <?php endif; ?>
    </header>
    <!-- .entry-header -->

    <div class="entry-content entry-con-fixh">
      <?php the_excerpt(); ?>

      <?php wp_link_pages( array( 'before'=>'
      <div class="page-links">' . __( 'Pages:', 'oblique' ), 'after' => '</div>', ) ); ?>
    </div>
    <!-- .entry-content -->
  </div>
  <?php if (!get_theme_mod( 'read_more')) : ?>
  <div class="read-more">
    <a href="<?php the_permalink(); ?>">
      <?php echo __( 'Continue reading &hellip;', 'oblique'); ?>
    </a>
  </div>
  <?php endif; ?>

</article>
<!-- #post-## -->

This is the code I am having. If I am having 50 posts how can I able to filter the images from that post

I tried using the following code

<?php if ( has_post_thumbnail() ) : ?>
  <div class="entry-thumb">
    <?php the_post_thumbnail( 'oblique-entry-thumb'); ?>
    <a class="thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><i class="fa fa-link"></i></a> 
  </div>
  <?php else : ?>
  <div class="entry-thumb">       
    <img src="http://placehold.it/500x500" />
    <a class="thumb-link" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><i class="fa fa-link"></i></a> 
  </div>    
  <?php endif; ?> 

but it wont display all images it shows only the dummy image that I placed

Upvotes: 2

Views: 93

Answers (1)

Pranav C Balan
Pranav C Balan

Reputation: 115212

If you want all post then you need to iterate over them and fetch the post thumbnail.

<?php
// define query fields
$query = array(
    // define post type to query
    'post_type' => 'post',
    // define posts count as -1, to fetch all 
    'posts_per_page' => -1
);
// query the post
$posts = query_posts($query);    
// iterate over the posts
foreach ($posts as $post): ?>
    <!-- echo the thumbnail -->
    <?php echo get_the_post_thumbnail($post->ID); ?>
    <!--    rest of the HTML -->
<?php endforeach; ?>

Upvotes: 1

Related Questions