plugin development - Get the current postpage URL with plain permalinks

I got it to work with this:$url = home_url( add_query_arg( array(), $wp->request ) );However, if the permalink is pla

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
  • You can either use 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) or var url = window.location.href; // Returns full URL (https://example/path/example.html) or var origin = window.location.origin; // Returns base URL (https://example) – Vishwa Commented May 20, 2019 at 4:01
  • @Vishwa was hoping for a php way : ) – mrKC.988 Commented May 20, 2019 at 4:16
  • get_permalink() should work for you. if you get problems in homepage, you can check with is_page(home). however, for your code to work properly, you'll need global $wp; before your code – Vishwa Commented May 20, 2019 at 4:40
  • Also try this global $wp; $current_url = home_url(add_query_arg(array($_GET), $wp->request)); – Vishwa Commented May 20, 2019 at 4:43
  • Possible duplicate of How to get URL of current page displayed? – norman.lol Commented May 20, 2019 at 6:49
 |  Show 1 more comment

3 Answers 3

Reset to default 2

Is 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

相关推荐

  • plugin development - Get the current postpage URL with plain permalinks

    I got it to work with this:$url = home_url( add_query_arg( array(), $wp->request ) );However, if the permalink is pla

    6小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信