How to give respective hyperlinks to respective images in a loop

Is there a way to add a hyperlink to each of the images so that when a user clicks a particular image, that the browser will navigate to a different URL? Each image would have a different url associated with it.

<?php 
    $display = $wpdb->get_results("SELECT name, image_path FROM table_name"); 
    foreach($display as $displays){ 
?>
<img src ="<?php echo $display->image_path ; ?>"/>
<?php  }  ?> 

So here I want to put a hyperlink for each image retrieved from the database, with the hyperlink navigating to a URL that displays the details about the image. How do I do it?

Related posts

Leave a Reply

6 comments

  1. image tag you cant add link , but you can put the image inside an anchor tag likewise,

    $display_books  =  $wpdb->get_results("SELECT bookname, image_path FROM book");
    
     foreach($display_books as $display)
     {
        <a href='<?php echo 'put your image_link dynamically ';?>' title='something'>
            <img src="<?php echo $display_books->image_path ; ?>"/>
        </a>
     }
    
  2. If you’ve got the associated link coming from db try this:

    <?php 
     $display_books= $wpdb->get_results("SELECT bookname, image_path, image_link FROM book"); 
    
        foreach($display_books as $display){ ?>
             <a href="<?php echo $display->image_link?>">
                 <img src="<?php echo $display->image_path ; ?>"/>
              </a>
    
    <?php  }  ?>
    
  3. Try this ,

    <?php 
        $display_books= $wpdb->get_results("SELECT bookname, image_path FROM book"); 
        foreach($display_books as $display){
        ?>
            <a href="<?php echo 'your_image_details_url_here';?>">
                <img src="<?php echo $display->image_path ; ?>"/>
            </a>
        <?php  
        }
    ?>
    
  4. Sure, just wrap each img tag in your loop in an a tag, with href pointing to the link URL you want.

    Assuming you can get the url link information the same way you’re getting the image path, it would look something like this:

    <?php 
     $display_books= $wpdb->get_results("SELECT bookname, image_path, image_url_link FROM book"); 
        foreach($display_books as $display){ ?>
             <a href="<?php echo $display->image_url_link; ?>">
               <img src="<?php echo $display->image_path ; ?>"/>
             </a>
    <?php  }  ?>
    
  5. <?php $display_books= $wpdb->get_results("SELECT bookname, image_path, Yourimage_hyperlink FROM book"); 
    foreach($display_books as $display){ ?>
         <a href="<?php echo $display->Yourimage_hyperlink ; ?>"><img src="<?php echo $display->image_path; ?>"/></a>
    <?php  } ?>
    
  6. <?php 
     $display_books = $wpdb->get_results("SELECT bookname, image_path FROM book"); 
        foreach($display_books as $display){ ?>
             <a href="destination.php">
             <img style="border:0;" src ="<?php echo $display->image_path ; ?>" alt="HTML tutorial" width="42" height="42"/>
             </a>
    
    <?php  }  ?>