WordPress函数get_permalink获取文章页面固定链接

WordPress函数get_permalink()获取文章页面固定链接

WordPress函数get_permalink()获取文章页面固定链接

  在 WordPress CMS内容管理系统中,get_permalink()是一个内置函数,用于获取帖子、页面或附件的永久链接。永久链接是用于访问网站前端帖子的 URL。

  推荐:SeedPro Comming Soon Pro插件WordPress即将推出页面插件

get_permalink()函数基本语法

$permalink = get_permalink( $post, $leavename );
// your custom code here
return $permalink;

描述

  显示下一篇文章页面链接

用法

get_permalink( $post,  $leavename );

参数

  $post

  ( int|WP_Post  ) ( 可选) 帖子 ID 或帖子对象。默认是全局的$post

  $leavename

 (bool)(可选)是否保留帖子名称或页面名称。默认值:false

  推荐:如何禁用右键单击WordPress图像

get_permalink()函数

  函数将返回帖子、页面或附件的永久链接。如果$post_id参数不是有效 ID,则函数将返回空字符串。get_permalink()函数对于获取帖子、页面和附件的永久链接非常有用。它可以用于各种不同的情况,例如当您创建指向您网站上的帖子、页面或附件的链接时。(源文件可参考这里

function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
	global $wpdb;

	$post = get_post();

	if ( ! $post || ! taxonomy_exists( $taxonomy ) ) {
		return null;
	}

	$current_post_date = $post->post_date;

	$join     = '';
	$where    = '';
	$adjacent = $previous ? 'previous' : 'next';

	if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
		// Back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and ".
		if ( false !== strpos( $excluded_terms, ' and ' ) ) {
			_deprecated_argument(
				__FUNCTION__,
				'3.3.0',
				sprintf(
					/* translators: %s: The word 'and'. */
					__( 'Use commas instead of %s to separate excluded terms.' ),
					"'and'"
				)
			);
			$excluded_terms = explode( ' and ', $excluded_terms );
		} else {
			$excluded_terms = explode( ',', $excluded_terms );
		}

		$excluded_terms = array_map( 'intval', $excluded_terms );
	}

	/**
	 * Filters the IDs of terms excluded from adjacent post queries.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * Possible hook names include:
	 *
	 *  - `get_next_post_excluded_terms`
	 *  - `get_previous_post_excluded_terms`
	 *
	 * @since 4.4.0
	 *
	 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
	 */
	$excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );

	if ( $in_same_term || ! empty( $excluded_terms ) ) {
		if ( $in_same_term ) {
			$join  .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
			$where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy );

			if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
				return '';
			}
			$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );

			// Remove any exclusions from the term array to include.
			$term_array = array_diff( $term_array, (array) $excluded_terms );
			$term_array = array_map( 'intval', $term_array );

			if ( ! $term_array || is_wp_error( $term_array ) ) {
				return '';
			}

			$where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';
		}

		if ( ! empty( $excluded_terms ) ) {
			$where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
		}
	}

	// 'post_status' clause depends on the current user.
	if ( is_user_logged_in() ) {
		$user_id = get_current_user_id();

		$post_type_object = get_post_type_object( $post->post_type );
		if ( empty( $post_type_object ) ) {
			$post_type_cap    = $post->post_type;
			$read_private_cap = 'read_private_' . $post_type_cap . 's';
		} else {
			$read_private_cap = $post_type_object->cap->read_private_posts;
		}

		/*
		 * Results should include private posts belonging to the current user, or private posts where the
		 * current user has the 'read_private_posts' cap.
		 */
		$private_states = get_post_stati( array( 'private' => true ) );
		$where         .= " AND ( p.post_status = 'publish'";
		foreach ( $private_states as $state ) {
			if ( current_user_can( $read_private_cap ) ) {
				$where .= $wpdb->prepare( ' OR p.post_status = %s', $state );
			} else {
				$where .= $wpdb->prepare( ' OR (p.post_author = %d AND p.post_status = %s)', $user_id, $state );
			}
		}
		$where .= ' )';
	} else {
		$where .= " AND p.post_status = 'publish'";
	}

	$op    = $previous ? '<' : '>';
	$order = $previous ? 'DESC' : 'ASC';

	/**
	 * Filters the JOIN clause in the SQL for an adjacent post query.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * Possible hook names include:
	 *
	 *  - `get_next_post_join`
	 *  - `get_previous_post_join`
	 *
	 * @since 2.5.0
	 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
	 *
	 * @param string       $join           The JOIN clause in the SQL.
	 * @param bool         $in_same_term   Whether post should be in the same taxonomy term.
	 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
	 * @param string       $taxonomy       Taxonomy. Used to identify the term used when `$in_same_term` is true.
	 * @param WP_Post      $post           WP_Post object.
	 */
	$join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );

	/**
	 * Filters the WHERE clause in the SQL for an adjacent post query.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * Possible hook names include:
	 *
	 *  - `get_next_post_where`
	 *  - `get_previous_post_where`
	 *
	 * @since 2.5.0
	 * @since 4.4.0 Added the `$taxonomy` and `$post` parameters.
	 *
	 * @param string       $where          The `WHERE` clause in the SQL.
	 * @param bool         $in_same_term   Whether post should be in the same taxonomy term.
	 * @param int[]|string $excluded_terms Array of excluded term IDs. Empty string if none were provided.
	 * @param string       $taxonomy       Taxonomy. Used to identify the term used when `$in_same_term` is true.
	 * @param WP_Post      $post           WP_Post object.
	 */
	$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );

	/**
	 * Filters the ORDER BY clause in the SQL for an adjacent post query.
	 *
	 * The dynamic portion of the hook name, `$adjacent`, refers to the type
	 * of adjacency, 'next' or 'previous'.
	 *
	 * Possible hook names include:
	 *
	 *  - `get_next_post_sort`
	 *  - `get_previous_post_sort`
	 *
	 * @since 2.5.0
	 * @since 4.4.0 Added the `$post` parameter.
	 * @since 4.9.0 Added the `$order` parameter.
	 *
	 * @param string $order_by The `ORDER BY` clause in the SQL.
	 * @param WP_Post $post    WP_Post object.
	 * @param string  $order   Sort order. 'DESC' for previous post, 'ASC' for next.
	 */
	$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );

	$query        = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
	$key          = md5( $query );
	$last_changed = wp_cache_get_last_changed( 'posts' );
	if ( $in_same_term || ! empty( $excluded_terms ) ) {
		$last_changed .= wp_cache_get_last_changed( 'terms' );
	}
	$cache_key = "adjacent_post:$key:$last_changed";

	$result = wp_cache_get( $cache_key, 'posts' );
	if ( false !== $result ) {
		if ( $result ) {
			$result = get_post( $result );
		}
		return $result;
	}

	$result = $wpdb->get_var( $query );
	if ( null === $result ) {
		$result = '';
	}

	wp_cache_set( $cache_key, $result, 'posts' );

	if ( $result ) {
		$result = get_post( $result );
	}

	return $result;
}

  推荐:NEX-Forms插件下载WordPress表单生成器插件+ Addons

如何使用get_permalink()

  显示当前帖子的固定链接,此代码显示 The Loop 中当前帖子的永久链接。

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        echo get_permalink();
    }
}

  检索特定帖子的固定链接,此代码通过其 ID 检索特定帖子的永久链接。

$post_id = 42; // Replace 42 with your desired post ID
$permalink = get_permalink( $post_id );
echo $permalink;

  检索帖子对象的固定链接,此代码使用 WP_Post 对象检索帖子的永久链接。

$post_object = get_post( 42 ); // Replace 42 with your desired post ID
$permalink = get_permalink( $post_object );
echo $permalink;

  使用帖子名称检索固定链接,此代码检索帖子的永久链接,将帖子名称保留在 URL 中。

$permalink = get_permalink( 42, true ); // Replace 42 with your desired post ID
echo $permalink;

  将永久链接显示为链接,此代码将永久链接显示为链接,使用帖子标题作为链接文本。

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        echo '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';
    }
}

  重定向到具有页面 ID 的页面

wp_redirect( get_permalink(1443) );
exit;

  推荐:[最新版]Asset CleanUp Pro插件免费下载WordPress性能优化插件

  推荐:WordPress函数使用手册


滚动至顶部