using the_permalink to echo an href into a

I’m trying to make a menu that automatically populates from a custom post type I have and I’m having some trouble getting it right If someone could point me in the right direction I would greatly appreciate it. Here is the code. The PHP in the <img src> is pulling the right info and sticking it in the right spot, the_permalink pulls the correct url but then it is putting the url above the <li> tag instead of in the href.

<ul class="product-menu">
              <?php
              $products = new WP_Query(
                $args = array(
                  'post_type' => 'product',
                  'post_status' => 'publish',
                )
              );
              while ( $products->have_posts() ) {
                $products->the_post();
                $post_thumbnail_id = get_post_thumbnail_id();
                $post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
                echo 
                '<li>
                  <a href="' . the_permalink() . '">
                    <img src="' . $post_thumbnail_url . '" alt="' . get_the_title() . '">
                  </a>
                </li>';
              } 
              /* Restore original Post Data */
              wp_reset_postdata();?>
              </ul>

2 Answers
2

the_permalink() prints url immediately and returns nothing, you should use get_the_permalink() function, which returns current post url.

Leave a Comment