简体   繁体   中英

WordPress get_permalink() inside Ad Inserter block not working

<?php 
  // query db once for permalink
  $permalink = urlencode(get_permalink());
?>

<a href='https://www.facebook.com/sharer/sharer.php?u=<?php echo $permalink; ?>' onclick='javascript:return (function(){window.open("https://www.facebook.com/sharer/sharer.php?u=<?php echo $permalink; ?>", "MsgWindow", "width=600, height=600, scrollbars=yes", false); return false;})();'>
<img onmouseover="this.style.opacity='0.8'" onmouseout="this.style.opacity='1'" style="margin-right:5px; width:40px; height:40px" src="data:image/png;base64,..." alt="Facebook Share" />
</a>

The above code executes in an Ad Inserter block but returns the wrong permalink. I have no other plugins installed for debugging purposes so that means no caching enabled.

The code executes w/o error but doesn't return the correct URL.

What could be wrong?

Try this:

get_the_permalink()

https://developer.wordpress.org/reference/functions/get_the_permalink/

And I don't think you have to urlencode() it.

Edit : It works only in the loop without any parameters. Outside the loop you have to pass the Post ID to the function.

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