当前浏览:首页 / WordPress函数 / get_permalink()

get_permalink( int|WP_Post $post, bool $leavename = false )

检索当前文章或文章ID的完整固定链接

permalink


参数

$post

(int|WP_Post)(可选) Post ID或Post对象。默认值为全局$post

$leavename

(bool)(可选) 是否保留文章名或页面名。

默认值: false


返回

(string|false) 返回固定链接URL,或者如果文章不存在,则为false。


说明

在插件或主题中,它可以早在setup_theme动作中使用。任何更早的使用,包括plugins_loaded,都会生成致命错误。

注意,当在没有ID参数的文章页面(index、archive等)的循环外使用时,它将返回循环中最后一篇文章的URL,而不是当前页面的固定链接。



源码

查看源码 官方文档


更新日志

版本描述
1.0.0开始引入

使用示例

  • 示例1

    传入一个post对象而不是ID
    这显示了如何使用页面标题而不是ID获取固定链接。

    <a href="<?php echo esc_url( get_permalink( get_page_by_title( 'Monthly Events' ) ) ); ?>"><?php esc_html_e( 'Monthly Events', 'textdomain' ); ?></a>
    
  • 示例2

    默认用法
    当前文章的固定链接(在循环中使用)。由于标签不显示固定链接,本例使用了PHP的echo命令。

    此文章的固定链接:

    echo get_permalink();
    
  • 示例3

    通过slug获取文章或页面(或任何内容)固定链接:

    function get_link_by_slug($slug, $type = 'post'){
      $post = get_page_by_path($slug, OBJECT, $type);
      return get_permalink($post->ID);
    }

    如果您正在使用polylang(像我通常做的那样),那么可以从任何一种语言slug开始使用永久链接(我通常从我的语言开始,通常是网站的默认语言):

    function get_link_by_slug($slug, $lang_slug = null, $type = 'post'){
      $post = get_page_by_path($slug, OBJECT, $type);
      $id = ($lang_slug) ? pll_get_post($post->ID, $lang_slug) : $post->ID;
      return get_permalink($id);
    }
  • 示例4
    <?php echo str_replace("http://www.mysite.com/","http://www.yoursite.com/",get_permalink()); ?>

    返回文章的固定链接,然后搜索http://www.mysite.com/并将其更改为http://www.yoursite.com/

  • 示例5

    链接到特定文章
    返回两个特定文章(文章ID 1和10)的固定链接,作为信息列表中的超文本链接。

    <ul>
    	<li><?php esc_html_e( 'MyBlog info:', 'textdomain' ); ?>
    		<ul>
    			<li><a href="<?php echo esc_url( get_permalink(1) ); ?>"><?php esc_html_e( 'About MyBlog', 'textdomain' ); ?></a></li>
    			<li><a href="<?php echo esc_url( get_permalink(10) ); ?>"><?php esc_html_e( 'About the owner', 'textdomain' ); ?></a></li>
    		</ul>
    	</li>
    </ul>
    
  • 示例6

    在循环外获取当前的文章/页面url。$post是一个全局变量。

    <?php echo get_permalink( $post->ID ); ?>
    
  • 示例7

    示例:Woocommerce商店页面permalink

    <?php echo get_permalink( wc_get_page_id( 'shop' ) ); ?>
  • 示例8

    根据页面ID重定向到页面:

    wp_redirect( get_permalink(1443) ); exit;
  • 示例9

    从当前子页面获取页面父级的固定链接(在循环内使用):

    while( have_posts() ): the_post();
    	get_permalink( wp_get_post_parent_id( get_the_ID() ) );
    endwhile;
    

    get_the_ID:获取当前页面的id
    wp_get_post_parent_id:获取当前页面的父页面id。

  • 示例10

    通过模板slug获取页面固定链接:

    function wpdocs_wp_get_page_url_by_template_slug( $template_slug ) {
        $url = null;
        $template = 'page-' . $template_slug . '.php';
    
        $pages = get_posts( array(
            'post_type' => 'page',
            'meta_query' => array(
                array(
                    'key' => '_wp_page_template',
                    'value' => $template,
                    'compare' => '=',
                )
            )
        ) );
    
        if ( isset( $pages[0] ) ) {
            $url = get_permalink( $pages[0]->ID );
        }
        return $url;
    }

    您可以在主题中的任何位置调用该函数,如下所示:

    //outputs url like "http(s)://(www.)(subdomain.)domain.com/shopping-cart-page-name__depends-on-permalink-settings/"
    echo wpdocs_wp_get_page_url_by_template_slug( 'shopping_cart' );

    注意:该函数仅检索使用给定模板的第一页。我写它是为了获取我的购物车的url等等,很明显,这些只使用了一次。我已经修改了我的模板下拉列表,这样它就不会显示某些模板,如果他们已经在任何其他文章中使用。如果要获取使用给定模板的所有文章,请修改以下函数:

    function wpdocs_wp_get_page_url_by_template_slug( $template_slug ) {
        $urls = array();
        $template = 'page-' . $template_slug . '.php';
    
        $pages = get_posts( array(
            'post_type' => 'page',
            'meta_query' => array(
                array(
                    'key' => '_wp_page_template',
                    'value' => $template,
                    'compare' => '=',
                )
            )
        ) );
    
        foreach ( $pages as $page ) {
            $urls[] = get_permalink( $page->ID );
        }
    
        return !empty( $urls ) ? $urls : false;
    }
  • 示例11

    permalink的另一种用法可以是

    echo "<script>window.location = '" . get_permalink($post_id) . "'</script>";
  • 示例12

    对于安全措施,请确保使用:

    esc_url( $url:string, $protocols:array|null, $_context:string );

    例子:

    echo esc_url( get_permalink(), 'textdomain' );

    参见 https://developer.wordpress.org/plugins/security/securing-output/