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?
Outside the post loop, you can use wordpress
get_permalink()
function for that. Inside the loop you can usethe_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
In WP, you can get permalink of the post with
get_permalink()
functionCodex – get_permalink()
You can use it like that:
If you are in TheLoop, which probably is the case, if you are in
single.php
, then you can usethe_permalink()
– it will echo the link directly.Codex – the_permalink()
Sorry for confusion.
Everything is working fine except for I am using Prepros live SASS compiler using custom port for preview
example.com:4000
which was causing the problem. As long as I switched to standard urlexample.com
, everything is working fine.Thanks for all on their precious contributions.
In your WordPress any template, page.php, single.php, serach.php or others page below code will working.
get_permalink
is WP function andget_the_ID()
is also a WP function that will get post id, page id. Try this code.try this function into functions.php relative permalink
output
/post/50/
or
or
or
See this link
See also