简体   繁体   中英

Adding Wordpress 'the_permalink()' to href not working

I don't know what I am doing wrong or what is causing this but it is just not outputting the WordPress permalink in the href of the a tag.

Below is my php code:

<?php 
     echo '<a href="' . the_permalink() . '"></a>';
?>

And this is what is being outputted in the HTML of the page:

https://domainname.co.uk/blogpost04/
<a href=""></a>

The permalink is correct, but it is just not inside the href of the a tag.

Thanks

You can't use the_permalink() inside an echo statement, in this case you have two echo statement's in each other.

please use it like this:

<?php 
     echo '<a href="' . get_permalink() . '"></a>';
?>

or as better alternative with printf and esc_url :

<?php 
     printf('<a href="%s">%s</a>', esc_url( get_permalink() ), 'your link text' );
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM