I got it to work with this:
$url = home_url( add_query_arg( array(), $wp->request ) );
However, if the permalink is plain, all I'm getting is the homepage url (instead of the post url).
So what's the best way to get the current post or page address link?
I got it to work with this:
$url = home_url( add_query_arg( array(), $wp->request ) );
However, if the permalink is plain, all I'm getting is the homepage url (instead of the post url).
So what's the best way to get the current post or page address link?
Share Improve this question edited May 28, 2019 at 8:13 Jacob Peattie 44.2k10 gold badges50 silver badges64 bronze badges asked May 20, 2019 at 3:52 mrKC.988mrKC.988 1072 silver badges14 bronze badges 6 | Show 1 more comment3 Answers
Reset to default 2Is there a reason why get_permalink() doesn't work for you? I'm unclear why you are trying to construct it manually.
Don't use get_permalink()
. That function is intended for use inside the loop, and only works for getting the link to posts. It will not work for taxonomy archives, date archives, search results, or the latest posts page.
Normally you can get the URL to the current page with home_url()
combined with $wp->request
:
global $wp;
echo home_url( $wp->request );
This is because home_url()
gets your site URL from the database, while $wp->request
holds the path for the current request, which will be the slug for pages, date for archives, etc.
The problem with plain permalinks is that they don't use the request path, because this requires the standard .htaccess setup. Instead WordPress uses the query string to determine what to load.
So what you need to do is get the current query vars and append those to the URL. Thankfully those are also available in $wp
, specifically as $wp->query_vars
. This is an array of vars and values, so you can pass it directly to add_query_arg();
:
global $wp;
echo add_query_arg( $wp->query_vars, home_url() );
Yeap, this should't work with plain format. and also just get_permalink()
also not going to solved your issue here. To solved your issue you need to first correct Page/Post id and WordPress default doesn't offer us any function to do that. So you need to make one to grab the correct id. example
/**
* Get correct page or post id
*
* @return int
*/
if ( !function_exists( 'get_the_real_ID' ) ) {
function get_the_real_ID() {
$post_id = 0;
if (in_the_loop()) {
$post_id = get_the_ID();
} else {
if ( is_front_page() && !is_home() ) {
$frontpage_id = get_option( 'page_on_front' );
$post_id = $frontpage_id;
} elseif ( !is_front_page() && is_home() ) {
$page_for_posts = get_option( 'page_for_posts' );
$post_id = $page_for_posts;
} else {
global $wp_query;
$page_object = $wp_query->get_queried_object();
if ( is_page() || is_singular() ) {
$post_id = $wp_query->get_queried_object_id();
} else {
$post_id = 0;
}
}
}
return $post_id;
}
}
Now just use this ID to grab your page/post url like this.
$id = get_the_real_ID();
$current_url = (empty($id)) ? home_url() : get_permalink($id);
I'm not sure that this is the best or perfect solution for your case or not but it should at-least will be work as you expected. I assume that you might change the permalink format or want to grab the proper url whatever format used? and uppon that I share this method. try and let me know.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745458199a4628585.html
get_permalink()
or use jQuery if it's something dynamic activity you're looking for.var pathname = window.location.pathname; // Returns path only (/path/example.html)
orvar url = window.location.href; // Returns full URL (https://example/path/example.html)
orvar origin = window.location.origin; // Returns base URL (https://example)
– Vishwa Commented May 20, 2019 at 4:01get_permalink()
should work for you. if you get problems in homepage, you can check withis_page(home)
. however, for your code to work properly, you'll needglobal $wp;
before your code – Vishwa Commented May 20, 2019 at 4:40global $wp; $current_url = home_url(add_query_arg(array($_GET), $wp->request));
– Vishwa Commented May 20, 2019 at 4:43