Advertisements

get_permalink

Definition:
function get_permalink($id = 0, $leavename = false) {}

Retrieve full permalink for current post or post ID.

Parameters

  • int $id: Optional. Post ID.
  • bool $leavename: Optional, defaults to false. Whether to keep post name or page name.

Defined filters

  • pre_post_link
    apply_filters('pre_post_link', $permalink, $post, $leavename)
  • post_link
    apply_filters('post_link', $permalink, $post, $leavename)

Source code

function get_permalink($id = 0, $leavename = false) {

	$rewritecode = array(

		'%year%',

		'%monthnum%',

		'%day%',

		'%hour%',

		'%minute%',

		'%second%',

		$leavename? '' : '%postname%',

		'%post_id%',

		'%category%',

		'%author%',

		$leavename? '' : '%pagename%',

	);



	if ( is_object($id) && isset($id->filter) && 'sample' == $id->filter ) {

		$post = $id;

		$sample = true;

	} else {

		$post = &get_post($id);

		$sample = false;

	}



	if ( empty($post->ID) )

		return false;



	if ( $post->post_type == 'page' )

		return get_page_link($post->ID, $leavename, $sample);

	elseif ( $post->post_type == 'attachment' )

		return get_attachment_link($post->ID);

	elseif ( in_array($post->post_type, get_post_types( array('_builtin' => false) ) ) )

		return get_post_permalink($post->ID, $leavename, $sample);



	$permalink = get_option('permalink_structure');



	$permalink = apply_filters('pre_post_link', $permalink, $post, $leavename);



	if ( '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {

		$unixtime = strtotime($post->post_date);



		$category = '';

		if ( strpos($permalink, '%category%') !== false ) {

			$cats = get_the_category($post->ID);

			if ( $cats ) {

				usort($cats, '_usort_terms_by_ID'); // order by ID

				$category = $cats[0]->slug;

				if ( $parent = $cats[0]->parent )

					$category = get_category_parents($parent, false, '/', true) . $category;

			}

			// show default category in permalinks, without

			// having to assign it explicitly

			if ( empty($category) ) {

				$default_category = get_category( get_option( 'default_category' ) );

				$category = is_wp_error( $default_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);

	}

	return apply_filters('post_link', $permalink, $post, $leavename);

}

1554

Advertisements

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: