Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress: echo current post permalink

Tags:

php

wordpress

I coding social sharing buttons in which I want to link current post in single.php template to social media share links.

The problem when I attach the permalink to share url it don`t give the full url of the post:

I tried http://twitter.com/share?url=<?php the_permalink() ?> but only gave output of /post/69 not http://example.com/post/69.

Any suggetions?


2 Answers

Outside the post loop, you can use wordpress get_permalink() function for that. Inside the loop you can use the_permalink($post->ID) although this echos the url straight out. here is the reference from wordpress.org.

https://developer.wordpress.org/reference/functions/get_permalink/ https://codex.wordpress.org/Function_Reference/the_permalink

like image 191
Raja Usman Mehmood Avatar answered Jul 22 '26 17:07

Raja Usman Mehmood


In WP, you can get permalink of the post with get_permalink() function

Codex - get_permalink()

You can use it like that:

global $post;
<?php echo get_permalink($post->ID);?>

If you are in TheLoop, which probably is the case, if you are in single.php, then you can use the_permalink() - it will echo the link directly.

Codex - the_permalink()

like image 37
pgk Avatar answered Jul 22 '26 19:07

pgk



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!