前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >wordpress获取当前页面链接

wordpress获取当前页面链接

作者头像
ytkah
发布2019-10-16 12:59:08
1.1K0
发布2019-10-16 12:59:08
举报
文章被收录于专栏:ytkahytkah

  我们知道wordpress的<?php the_permalink(); ?>和<?php echo get_permalink(); ?>可以获取页面链接,但是有些比较复杂的环境可能输出的链接是错误的,那要如何获取当前页面链接呢?可以试一下用下面的方法

代码语言:javascript
复制
<?php
global $wp; 
$current_url = home_url(add_query_arg(array(),$wp->request)); 
echo $current_url; 
?>

  还有更复杂的方法

代码语言:javascript
复制
function get_current_archive_link( $paged = true ) { 
        $link = false; 
 
        if ( is_front_page() ) { 
                $link = home_url( '/' ); 
        } else if ( is_home() && "page" == get_option('show_on_front') ) { 
                $link = get_permalink( get_option( 'page_for_posts' ) ); 
        } else if ( is_tax() || is_tag() || is_category() ) { 
                $term = get_queried_object(); 
                $link = get_term_link( $term, $term->taxonomy ); 
        } else if ( is_post_type_archive() ) { 
                $link = get_post_type_archive_link( get_post_type() ); 
        } else if ( is_author() ) { 
                $link = get_author_posts_url( get_query_var('author'), get_query_var('author_name') ); 
        } else if ( is_archive() ) { 
                if ( is_date() ) { 
                        if ( is_day() ) { 
                                $link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') ); 
                        } else if ( is_month() ) { 
                                $link = get_month_link( get_query_var('year'), get_query_var('monthnum') ); 
                        } else if ( is_year() ) { 
                                $link = get_year_link( get_query_var('year') ); 
                        }                                                
                } 
        } 
 
        if ( $paged && $link && get_query_var('paged') > 1 ) { 
                global $wp_rewrite; 
                if ( !$wp_rewrite->using_permalinks() ) { 
                        $link = add_query_arg( 'paged', get_query_var('paged'), $link ); 
                } else { 
                        $link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . get_query_var('paged'), 'archive' ); 
                } 
        } 
        return $link; 
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-10-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档