在WordPress项目开发过程,极可能须要获取WordPress 各种页面的连接,包括首页、文章页、Page页面、存档页面等等,今天倡萌就简单分享下获取 WordPress 各种页面的连接的方法。php
获取文章或页面连接
直接输出文章或页面的连接:html
- <?php the_permalink(); ?>
返回文章或页面的连接,以供调用:wordpress
能够使用 echo 输出,结果和直接使用 the_permalink() 同样:函数
- <?php echo get_permalink(); ?>
获取存档页面连接
- 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;
- }
该函数能够输出首页、分类法(自定义分类法、标签、分类)、自定义文章类型的存档页面、做者存档页面、日期存档页面 的连接,包含分页。post
获取当前页面连接
若是你不想判断页面类型,只想输出当前页面的连接,能够使用下面的代码:url
- <?php
- global $wp;
- $current_url = home_url(add_query_arg(array(),$wp->request));
- echo $current_url;
- ?>
好了,暂且说到这里。若是你们有什么补充,欢迎留言分享,谢谢。spa
参考资料:xml
http://wordpress.stackexchange.com/questions/29512/permalink-for-category-pages-and-postshtm
http://stephenharris.info/how-to-get-the-current-url-in-wordpress/开发
本文连接:https://liuyanzhao.com/4671.html