WordPress函数get_the_permalink()检索当前帖子或帖子ID永久链接

WordPress函数get_the_permalink()检索当前帖子或帖子ID永久链接

WordPress函数get_the_permalink()检索当前帖子或帖子ID永久链接

  在 WordPress CMS内容管理系统中,get_the_permalink()是一个内置函数,可以在循环中检索当前帖子或页面的完整永久链接(永久链接)。它生成特定帖子或页面的完整 URL,包括域名。

  • 它是一个模板标签函数,这意味着它只能在显示帖子时在循环中使用。在循环之外它将不起作用。
  • 它不接受任何参数 – 它只是返回当前帖子的永久链接。
  • 它返回的永久链接基于 WordPress 中设置 > 永久链接下设置的永久链接结构。
  • 它会自动返回带有永久链接结构中的帖子 ID 或页面名称 slug 的 URL。
  • 返回的 URL 被输出,而不是回显,因此不需要回显 get_the_permalink()。
  • 它对于在模板中显示永久链接 URL 非常有用,例如在帖子摘录、相关帖子等上

  推荐:[最新版]Swift Performance性能插件WordPress优化插件

get_the_permalink()函数基本语法

描述

  显示当前帖子/页面的永久链接

用法

get_the_permalink( $post, $leavename );
  • $post (int|WP_Post, 可选) – 帖子 ID 或帖子对象。默认是全局的$post
  • $leavename (bool, 可选) – 是否保留帖子名称或页面名称。默认:false

  推荐:WordPress函数get_the_terms()检索帖子附加的分类术语

get_the_permalink()函数

  get_the_permalink()功能与各种 WordPress 内容类型兼容,包括帖子、页面、自定义帖子类型和附件。考虑永久链接结构设置以准确生成 URL。是 WordPress 主题和插件内导航和链接的基本功能(源文件可参考这里

function get_the_permalink( $post = 0, $leavename = false ) {
	return get_permalink( $post, $leavename );
}

  推荐:WordPress函数email_exists()确定给定的电子邮件是否存在

如何使用get_the_permalink()

  获取当前帖子的永久链接,此代码在循环中检索当前帖子的永久链接。

// Get the permalink
$permalink = get_the_permalink();
// Display the permalink
echo $permalink;

  通过ID获取特定帖子的永久链接,此代码检索ID为帖子的永久链接42。

// Get the permalink of post with ID 42
$permalink = get_the_permalink(42);
// Display the permalink
echo $permalink;

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

// Get the WP_Post object for post with ID 42
$post_object = get_post(42);
// Get the permalink using the WP_Post object
$permalink = get_the_permalink($post_object);
// Display the permalink
echo $permalink;

  获取永久链接而不更改帖子或页面名称,此代码检索当前帖子的永久链接,而不更改帖子或页面名称。

// Get the permalink without altering post or page name
$permalink = get_the_permalink(null, true);
// Display the permalink
echo $permalink;

  将永久链接显示为链接,此代码检索当前帖子的永久链接并将其显示为链接。

$permalink = get_the_permalink();
echo '<a href="' . $permalink . '">Read More</a>';

  推荐:WP Speed of Light插件使用教程WordPress速度优化插件

  推荐:WordPress函数使用手册

5/5 - (1 vote)

Claude、Netflix、Midjourney、ChatGPT Plus、PS、Disney、Youtube、Office 365、多邻国Plus账号购买,ChatGPT API购买,优惠码XDBK,用户购买的时候输入优惠码可以打95折

Chatgpt-Plus注册购买共享账号
滚动至顶部