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

get_pending_comments_num

Definition:
function get_pending_comments_num( $post_id ) {}

Get the number of pending comments on a post or posts

Parameters

  • int|array $post_id: Either a single Post ID or an array of Post IDs

Return values

returns:Either a single Posts pending comments as an int or an array of ints keyed on the Post IDs

Source code

function get_pending_comments_num( $post_id ) {

	global $wpdb;



	$single = false;

	if ( !is_array($post_id) ) {

		$post_id_array = (array) $post_id;

		$single = true;

	} else {

		$post_id_array = $post_id;

	}

	$post_id_array = array_map('intval', $post_id_array);

	$post_id_in = "'" . implode("', '", $post_id_array) . "'";



	$pending = $wpdb->get_results( "SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM $wpdb->comments WHERE comment_post_ID IN ( $post_id_in ) AND comment_approved = '0' GROUP BY comment_post_ID", ARRAY_A );



	if ( $single ) {

		if ( empty($pending) )

			return 0;

		else

			return absint($pending[0]['num_comments']);

	}



	$pending_keyed = array();



	// Default to zero pending for all posts in request

	foreach ( $post_id_array as $id )

		$pending_keyed[$id] = 0;



	if ( !empty($pending) )

		foreach ( $pending as $pend )

			$pending_keyed[$pend['comment_post_ID']] = absint($pend['num_comments']);



	return $pending_keyed;

}

1552

get_parent_post_rel_link

Definition:
function get_parent_post_rel_link($title = '%title') {}

Get parent post relational link.

Parameters

  • string $title: Optional. Link title format.

Defined filters

  • the_title
    apply_filters('the_title', $title, $post->ID)
  • parent_post_rel_link
    apply_filters( "parent_post_rel_link", $link )

Source code

function get_parent_post_rel_link($title = '%title') {

	if ( ! empty( $GLOBALS['post'] ) && ! empty( $GLOBALS['post']->post_parent ) )

		$post = & get_post($GLOBALS['post']->post_parent);



	if ( empty($post) )

		return;



	$date = mysql2date(get_option('date_format'), $post->post_date);



	$title = str_replace('%title', $post->post_title, $title);

	$title = str_replace('%date', $date, $title);

	$title = apply_filters('the_title', $title, $post->ID);



	$link = "<link rel='up' title='";

	$link .= esc_attr( $title );

	$link .= "' href='" . get_permalink($post) . "' />\n";



	return apply_filters( "parent_post_rel_link", $link );

}

1550

get_page_uri

Definition:
function get_page_uri($page) {}

Builds URI for a page.
Sub pages will be in the "directory" under the parent page post name.

Parameters

  • mixed $page: Page object or page ID.

Return values

returns:Page URI.

Source code

function get_page_uri($page) {

	if ( ! is_object($page) )

		$page = get_page($page);

	$uri = $page->post_name;



	// A page cannot be it's own parent.

	if ( $page->post_parent == $page->ID )

		return $uri;



	while ($page->post_parent != 0) {

		$page = get_page($page->post_parent);

		$uri = $page->post_name . "/" . $uri;

	}



	return $uri;

}

1548

get_page_templates

Definition:
function get_page_templates() {}

Get the Page Templates available in this theme

Return values

returns:Key is the template name, value is the filename of the template

Source code

function get_page_templates() {

	$themes = get_themes();

	$theme = get_current_theme();

	$templates = $themes[$theme]['Template Files'];

	$page_templates = array();



	if ( is_array( $templates ) ) {

		$base = array( trailingslashit(get_template_directory()), trailingslashit(get_stylesheet_directory()) );



		foreach ( $templates as $template ) {

			$basename = str_replace($base, '', $template);



			// don't allow template files in subdirectories

			if ( false !== strpos($basename, '/') )

				continue;



			if ( 'functions.php' == $basename )

				continue;



			$template_data = implode( '', file( $template ));



			$name = '';

			if ( preg_match( '|Template Name:(.*)$|mi', $template_data, $name ) )

				$name = _cleanup_header_comment($name[1]);



			if ( !empty( $name ) ) {

				$page_templates[trim( $name )] = $basename;

			}

		}

	}



	return $page_templates;

}

1546