WordPress函数the_permalink()显示当前文章固定链接

WordPress函数the_permalink()显示当前文章固定链接

WordPress函数the_permalink()显示当前文章固定链接

  在 WordPress CMS内容管理系统中,the_permalink()是一个内置函数,用于获取帖子的永久链接。固定链接是用于访问站点前端帖子的 URL。该函数采用可选参数 ,$id它是帖子的 ID。如果$id未指定参数,该函数将获取当前帖子的永久链接。

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

the_permalink()函数基本语法

add_filter('the_permalink', 'your_custom_function', 10, 2);
function your_custom_function($permalink, $post) {
    // your custom code here
    return $permalink;
}

描述

  显示当前帖子的永久链接

用法

the_permalink( $post );

参数

  $post

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

挂钩

apply_filters ( 'the_permalink',  $permalink ,  $post )

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

the_permalink()函数

  此标签必须在The Loop内,通常用于在显示帖子时显示每个帖子的永久链接。由于此模板标签仅限于显示正在处理的帖子的永久链接,因此您不能使用它来显示指向博客上任意帖子的永久链接。参考get_permalink() 如果你想获得帖子的永久链接,给定其唯一的帖子 ID(源文件可参考这里

function the_permalink( $post = 0 ) {
	/**
	 * Filters the display of the permalink for the current post.
	 *
	 * @since 1.5.0
	 * @since 4.4.0 Added the `$post` parameter.
	 *
	 * @param string      $permalink The permalink for the current post.
	 * @param int|WP_Post $post      Post ID, WP_Post object, or 0. Default 0.
	 */
	echo esc_url( apply_filters( 'the_permalink', get_permalink( $post ), $post ) );
}

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

如何使用the_permalink()

  添加自定义查询参数,将自定义查询参数添加ref到永久链接。

add_filter('the_permalink', 'add_custom_query_parameter', 10, 2);
function add_custom_query_parameter($permalink, $post) {
    $permalink = add_query_arg('ref', 'custom_value', $permalink);
    return $permalink;
}

  添加 UTM 活动,将 UTM 活动添加到永久链接以进行跟踪

add_filter('the_permalink', 'add_utm_campaign', 10, 2);
function add_utm_campaign($permalink, $post) {
    $permalink = add_query_arg('utm_campaign', 'your_campaign', $permalink);
    return $permalink;
}

  将永久链接更改为自定义 URL,将帖子永久链接重定向到存储在自定义字段中的外部 URL

add_filter('the_permalink', 'redirect_to_custom_url', 10, 2);
function redirect_to_custom_url($permalink, $post) {
    $custom_url = get_post_meta($post->ID, 'custom_url', true);
    if (!empty($custom_url)) {
        $permalink = $custom_url;
    }
    return $permalink;
}

  从固定链接中删除类别库,从固定链接中删除类别库。

add_filter('the_permalink', 'remove_category_base_from_permalink', 10, 2);
function remove_category_base_from_permalink($permalink, $post) {
    $category_base = get_option('category_base');
    if ($category_base) {
        $permalink = str_replace($category_base . '/', '', $permalink);
    }
    return $permalink;
}

  使固定链接散列,根据帖子 ID 生成散列固定链接。

add_filter('the_permalink', 'hashed_permalink', 10, 2);
function hashed_permalink($permalink, $post) {
    $hashed_permalink = home_url('/') . '#' . md5($post->ID);
    return $hashed_permalink;
}

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

  推荐:WordPress函数使用手册


晓得博客,版权所有丨如未注明,均为原创
晓得博客 » WordPress函数the_permalink()显示当前文章固定链接

转载请保留链接:https://www.pythonthree.com/wordpress-the-permalink/

滚动至顶部