Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add permalink into WP_Query in Wordpress Theme

I want to add Blog Page in front-page.php.I want to show only post title with link to actual post.I dont know where to put the_permalink in my code.My code like below:

 <div class="profile">               
                <?php 
                  $query = new WP_query( 'pagename=blog' ); 
                    if ( $query->have_posts() ) {
                    while ( $query->have_posts() ){
                    $query->the_post();
                    echo '<h2 class="text-center">' . get_the_title() . '</h2>';                                
                       }
                     }
                  wp_reset_postdata();
                 ?>   
 </div><!--end blog-content -->     

When I put ,the hyperlink goes to domain.com/get_permalink() not to actual url.

Example the actual url is domain.com/abc. When I click to the post title,it goes to domain.com/get_permalink()

Anyone can help me to solve my problem?

like image 304
Junior WP Developer Avatar asked Feb 11 '26 05:02

Junior WP Developer


1 Answers

May be you are using get_permalink in wrong way. Try following code.

<div class="profile">
  <?php
  $query = new WP_query( 'pagename=blog' );
  if ( $query->have_posts() ) {
    while ( $query->have_posts() ){
      $query->the_post();
      echo '<h2 class="text-center"><a href="' . get_permalink() . '">';
      the_title();
      echo '</a></h2>';
    }
  }
  wp_reset_postdata();
  ?>
</div><!--end blog-content -->
like image 128
Nilambar Sharma Avatar answered Feb 13 '26 19:02

Nilambar Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!