get_permalink( int|WP_Post $post, bool $leavename = false )
Retrieves the full permalink for the current post or post ID.
Contents
Description Description
Parameters Parameters
- $post
-
(int|WP_Post) (Optional) Post ID or post object. Default is the global
$post
. - $leavename
-
(bool) (Optional) Whether to keep post name or page name.
Default value: false
Return Return
(string|false) The permalink URL or false if post does not exist.
Source Source
File: wp-includes/link-template.php
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | function get_permalink( $post = 0, $leavename = false ) { $rewritecode = array ( '%year%' , '%monthnum%' , '%day%' , '%hour%' , '%minute%' , '%second%' , $leavename ? '' : '%postname%' , '%post_id%' , '%category%' , '%author%' , $leavename ? '' : '%pagename%' , ); if ( is_object ( $post ) && isset( $post ->filter ) && 'sample' == $post ->filter ) { $sample = true; } else { $post = get_post( $post ); $sample = false; } if ( empty ( $post ->ID ) ) { return false; } if ( $post ->post_type == 'page' ) { return get_page_link( $post , $leavename , $sample ); } elseif ( $post ->post_type == 'attachment' ) { return get_attachment_link( $post , $leavename ); } elseif ( in_array( $post ->post_type, get_post_types( array ( '_builtin' => false ) ) ) ) { return get_post_permalink( $post , $leavename , $sample ); } $permalink = get_option( 'permalink_structure' ); /** * Filters the permalink structure for a post before token replacement occurs. * * Only applies to posts with post_type of 'post'. * * @since 3.0.0 * * @param string $permalink The site's permalink structure. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. */ $permalink = apply_filters( 'pre_post_link' , $permalink , $post , $leavename ); if ( '' != $permalink && ! in_array( $post ->post_status, array ( 'draft' , 'pending' , 'auto-draft' , 'future' ) ) ) { $unixtime = strtotime ( $post ->post_date ); $category = '' ; if ( strpos ( $permalink , '%category%' ) !== false ) { $cats = get_the_category( $post ->ID ); if ( $cats ) { $cats = wp_list_sort( $cats , array ( 'term_id' => 'ASC' , ) ); /** * Filters the category that gets used in the %category% permalink token. * * @since 3.5.0 * * @param WP_Term $cat The category to use in the permalink. * @param array $cats Array of all categories (WP_Term objects) associated with the post. * @param WP_Post $post The post in question. */ $category_object = apply_filters( 'post_link_category' , $cats [0], $cats , $post ); $category_object = get_term( $category_object , 'category' ); $category = $category_object ->slug; if ( $category_object ->parent ) { $category = get_category_parents( $category_object ->parent, false, '/' , true ) . $category ; } } // show default category in permalinks, without // having to assign it explicitly if ( empty ( $category ) ) { $default_category = get_term( get_option( 'default_category' ), 'category' ); if ( $default_category && ! is_wp_error( $default_category ) ) { $category = $default_category ->slug; } } } $author = '' ; if ( strpos ( $permalink , '%author%' ) !== false ) { $authordata = get_userdata( $post ->post_author ); $author = $authordata ->user_nicename; } $date = explode ( ' ' , date ( 'Y m d H i s' , $unixtime ) ); $rewritereplace = array ( $date [0], $date [1], $date [2], $date [3], $date [4], $date [5], $post ->post_name, $post ->ID, $category , $author , $post ->post_name, ); $permalink = home_url( str_replace ( $rewritecode , $rewritereplace , $permalink ) ); $permalink = user_trailingslashit( $permalink , 'single' ); } else { // if they're not using the fancy permalink option $permalink = home_url( '?p=' . $post ->ID ); } /** * Filters the permalink for a post. * * Only applies to posts with post_type of 'post'. * * @since 1.5.0 * * @param string $permalink The post's permalink. * @param WP_Post $post The post in question. * @param bool $leavename Whether to keep the post name. */ return apply_filters( 'post_link' , $permalink , $post , $leavename ); } |
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
1.0.0 | Introduced. |
More Information More Information
In a Plugin or Theme, it can be used as early as the setup_theme
Action. Any earlier usage, including plugins_loaded
, generates a Fatal Error.
Note that when used outside The Loop on a posts page (index, archive, etc.) without the ID parameter, it will return the URL of the last post in The Loop, not the permalink for the current page.
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Pass in a post object instead of an ID
This shows how you can you can get the permalink with the page title instead of the ID.
<a href=
"<?php echo esc_url( get_permalink( get_page_by_title( 'Monthly Events' ) ) ); ?>"
><?php esc_html_e(
'Monthly Events'
,
'textdomain'
); ?></a>
Get post or page (or whatever) permalink by slug:
function
get_link_by_slug(
$slug
,
$type
=
'post'
){
$post
= get_page_by_path(
$slug
, OBJECT,
$type
);
return
get_permalink(
$post
->ID);
}
If you are using polylang (like I usually do), you can get the permalink starting from whichever language slug (I generally start from my language, usually the websites’s default):
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
);
}
Default Usage
The permalink for current post (used within The Loop). As the tag does not display the permalink, the example uses the PHP echo command.
Permalink for this post:
echo
get_permalink();
Link to Specific Post
Returns the permalinks of two specific posts (post IDs 1 and 10) as hypertext links within an informational list.
<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>
<?php
echo
str_replace
(
"http://www.mysite.com/"
,
"http://www.yoursite.com/"
,get_permalink()); ?>
Return the permalink of the post, then search for http://www.mysite.com/ and change it to http://www.yoursite.com/
Get current post/page url outside loop. $post is a global variable.
<?php
echo
get_permalink(
$post
->ID ); ?>
Woocommerce shop page permalink
<?php
echo
get_permalink( wc_get_page_id(
'shop'
) ); ?>