0

I'm fetching data from custom post type Products using product-template.php like this

   <div class="container" style="margin-top: 20px;">
      <h1>Our Products</h1>
        <?php
         $args=array('post_type' => 'products');
         $query= new WP_Query($args);                               
         while ($query-> have_posts() ) : $query->the_post()?>
         <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12">
             <h1><?php echo the_title();?></h1>
             <?php the_post_thumbnail( 'full', array( 'class' => 'innerimages') );?>
         </div>

     <?php                                
        endwhile;
     ?>
   </div> 

now what i want to do is title should be clickable and once clicked on next page i should be able to get all the details about clicked product how do i do this?

1
  • 1
    the_permalink() within the loop should work. And after endwhile use wp_reset_postdata() to reset your custom WP_Query(). Oct 21, 2015 at 7:56

1 Answer 1

1

You can do this using,

<div class="container" style="margin-top: 20px;">
      <h1>Our Products</h1>
        <?php
         $args=array('post_type' => 'products');
         $query= new WP_Query($args);                               
         while ($query-> have_posts() ) : $query->the_post()?>
         <div class="col-lg-2 col-md-2 col-sm-4 col-xs-12">
             <!-- Add permalink as below -->
             <a href="<?php the_permalink(); ?>"><h1><?php echo the_title();?></h1></a>
             <?php the_post_thumbnail( 'full', array( 'class' => 'innerimages') );?>
         </div>

     <?php                                
        endwhile;
     ?>
   </div> 

And then for details you have to create 'single-products.php' file and in that access your post details like,

<div>
    <?php the_post(); ?>
    <h1><?php the_title(); ?></h1>
    <div><?php the_content(); ?></div>

    .......
</div>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.