简体   繁体   English

将 Wordpress 'the_permalink()' 添加到 href 不起作用

[英]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.我不知道我做错了什么或导致此问题的原因,但它只是没有在 a 标签的 href 中输出 WordPress 永久链接。

Below is my php code:下面是我的php代码:

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

And this is what is being outputted in the HTML of the page:这就是页面 HTML 中输出的内容:

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

The permalink is correct, but it is just not inside the href of the a tag.永久链接是正确的,但它只是不在 a 标签的 href 内。

Thanks谢谢

You can't use the_permalink() inside an echo statement, in this case you have two echo statement's in each other.您不能在 echo 语句中使用the_permalink() ,在这种情况下,您有两个相互的 echo 语句。

please use it like this:请像这样使用它:

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

or as better alternative with printf and esc_url :或者作为printfesc_url的更好替代方案:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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