Benedikt W
Benedikt W

Reputation: 183

How to add post link to thumbnail in wordpress?

I am using the following loop in my index.php to show the posts with a thumbnail:

<main id="main">
            
                <?php 
                // the query
                $args = array('posts_per_page' => 10 );
                $the_query = new WP_Query( $args ); 
            
                ?>
            
                <?php if ( $the_query->have_posts() ) { ?>
            
                    <!-- loop -->
            
                    <?php while ( $the_query->have_posts() ) {
                     
                                $the_query->the_post(); ?>
           <article id="post"> 
                  
                        <div id="thumbnail">
                        
                            <?php
                            if ( has_post_thumbnail() ) { ?>
                                 <?php the_post_thumbnail(); } ?>
                    
                    </div>
                   
                   <h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2>
                   
                   <div class="entry">
                   
                        <?php the_excerpt(); ?>
                        
                   </div>
                 
           
           </article>
           
	
    
                <?php } } else { ?>
                <p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
                <?php }  ?>
    			
               <!-- end of the loop -->
	
    
               <?php wp_reset_postdata(); ?>
        </main>
    

How can I add a permalink to the thumbnail? When the user clicks on it he should be directed to the post. At the moment, nothing is happening.

Thanks for your answers.

EDIT: I added the whole loop because the answer of Deepti produces an error. Maybe someone could help me with that.

Upvotes: 0

Views: 78

Answers (1)

Deepti chipdey
Deepti chipdey

Reputation: 1177

<?php if ( has_post_thumbnail() ) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
    <?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>

Upvotes: 3

Related Questions