This is the code I am using:
<?php echo str_replace( home_url(), '', get_permalink($post->ID) ); ?>
What it does is output the permalink as a relative URL i.e. only the slug. For instance, if the permalink is /
, the relative URL output by the code would look like /2012/01/post-title/
.
Problem: All Posts and Pages show the right permalink, which is great. But all other pages (including Home, Search and Archives) show the relative URL of the first post and not that of the respective pages. Any idea why? What am I doing wrong here?
Reference: Get page permalink without wpurl
EDIT: Here's what else I've tried:
In functions.php
function get_relative_permalink( $url ) {
$url = get_permalink();
return str_replace( home_url(), "", $url );
}
In header.php
<link rel="alternate" hreflang="en-IN" href="/<?php echo get_relative_permalink(); ?>" />
Same problem with this as well. But this one shows a not-so-informative error too.
This question already has answers here: Get page permalink without wpurl (5 answers) Closed 8 years ago.This is the code I am using:
<?php echo str_replace( home_url(), '', get_permalink($post->ID) ); ?>
What it does is output the permalink as a relative URL i.e. only the slug. For instance, if the permalink is http://example/2012/01/post-title/
, the relative URL output by the code would look like /2012/01/post-title/
.
Problem: All Posts and Pages show the right permalink, which is great. But all other pages (including Home, Search and Archives) show the relative URL of the first post and not that of the respective pages. Any idea why? What am I doing wrong here?
Reference: Get page permalink without wpurl
EDIT: Here's what else I've tried:
In functions.php
function get_relative_permalink( $url ) {
$url = get_permalink();
return str_replace( home_url(), "", $url );
}
In header.php
<link rel="alternate" hreflang="en-IN" href="http://in.example/<?php echo get_relative_permalink(); ?>" />
Same problem with this as well. But this one shows a not-so-informative error too.
Share Improve this question edited Apr 13, 2017 at 12:37 CommunityBot 1 asked Aug 28, 2012 at 13:09 its_meits_me 4,5129 gold badges66 silver badges114 bronze badges 5 |3 Answers
Reset to default 13Use $_SERVER['REQUEST_URI']
instead of get_permalink()
to grab the current URL. get_permalink
will give you the full address of the current post, not the address of the URL visited.
e.g. for example/test/page echo $_SERVER['REQUEST_URI'];
prints /test/page
Note that this doesn't include the hashtag, as that part never gets sent to the server, and it also doesn't include ?foo=bar
type parameters, those are in the $_GET
array.
I use
str_replace(home_url(), '', get_permalink());
If site root is not /
This works for me:
function force_relative_url ($url)
{
return preg_replace ('/^(http)?s?:?\/\/[^\/]*(\/?.*)$/i', '$2', '' . $url);
}
To use it on a permalink:
$relative_permalink = force_relative_url (get_permalink ($post->ID));
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742267705a4412114.html
global $post
before your above code? – Joshua Abenazer Commented Aug 28, 2012 at 13:31<link rel="alternate" hreflang="en-IN" href="http://in.example/2012/01/post-title/" />
— but the actual permalink is like this:http://example/2012/01/post-title/
. (Reading this will give you a better idea of what I am doing.) – its_me Commented Aug 28, 2012 at 13:41get_permalink()
is for getting instances of classWP_Post
links (posts, pages, custom post types, ... )... for archives, tags, etc - just categories in general - instances of classWP_Term
- it isget_term_link()
, for home it ishome_url()
– jave.web Commented Mar 18, 2018 at 19:49