钩子文档

post_type_link

💡 云策文档标注

概述

post_type_link 是一个 WordPress 过滤器,用于在 get_post_permalink() 函数返回之前修改自定义文章类型的固定链接 URL。它允许开发者基于文章属性动态调整链接。

关键要点

  • post_type_link 过滤器应用于自定义文章类型的固定链接,参数包括 $post_link(链接字符串)、$post(WP_Post 对象)、$leavename(是否保留文章名)和 $sample(是否为示例链接)。
  • 该过滤器在 get_post_permalink() 函数中调用,用于检索或修改链接。
  • 常见用例包括根据文章元字段或格式重定向链接,或添加查询参数。

代码示例

// 示例1:使用元字段和文章格式设置外部链接
if ( ! function_exists( 'set_external_url_post_link' ) ):
    function set_external_url_post_link( $post_link, $post ) {
        if ( 'custom_post_type' === $post->post_type ) {
            $external_url = get_post_meta( $post->ID, 'MY_META_KEY', true );
            if ( 'link' == get_post_format( $post->ID ) && ! empty( $external_url ) ) {
                return $external_url;
            }
        }
        return $post_link;
    }
    add_filter( 'post_type_link', 'set_external_url_post_link', 10, 2 );
endif;
// 示例2:为自定义文章类型链接追加查询字符串
function append_query_string( $url, $post ) {
    if ( 'my_custom_post_type' == get_post_type( $post ) ) {
        return add_query_arg( $_GET, $url );
    }
    return $url;
}
add_filter( 'post_type_link', 'append_query_string', 10, 2 );

📄 原文内容

Filters the permalink for a post of a custom post type.

Parameters

$post_linkstring
The post’s permalink.
$postWP_Post
The post in question.
$leavenamebool
Whether to keep the post name.
$samplebool
Is it a sample permalink.

More Information

post_type_link is a filter applied to the permalink URL for a post or custom post type prior to being returned by the function get_post_permalink() .

Source

return apply_filters( 'post_type_link', $post_link, $post, $leavename, $sample );

Changelog

Version Description
3.0.0 Introduced.

User Contributed Notes

  1. Skip to note 3 content

    External Link for custom post type using meta field and post format ( Link ).

    if ( ! function_exists( 'set_external_url_post_link' ) ):
    	function set_external_url_post_link( $post_link, $post ) {
    		if ( 'custom_post_type' === $post->post_type ) {
    			$external_url  = get_post_meta( $post->ID, 'MY_META_KEY', true );
    			if ( 'link' == get_post_format( $post->ID ) && ! empty( $external_url ) ) {
    				return $external_url;
    			}
    		}
    		return $post_link;
    	}
    	add_filter( 'post_type_link', 'set_external_url_post_link', 10, 2 );
    endif;
    
    if ( ! function_exists( 'redirect_url_post_link' ) ):
    	function redirect_url_post_link() {
    		global $post;
    		if ( is_single() && ( 'custom_post_type' === get_post_type( $post ) ) ) {
    			$external_url  = get_post_meta( $post->ID, 'MY_META_KEY', true );
    			if ( 'link' == get_post_format( $post->ID ) && ! empty( $external_url ) ) {
    				wp_redirect( $external_url );
    				exit();
    			}
    		}
    	}
    	add_action('template_redirect', 'redirect_url_post_link');
    endif;

  2. Skip to note 4 content

    Example migrated from Codex:

    Append the query string for the custom post type ‘my_custom_post_type’ permalink URLs ( uses add_query_arg() and get_post_type() ):

    function append_query_string( $url, $post ) {
        if ( 'my_custom_post_type' == get_post_type( $post ) ) {
            return add_query_arg( $_GET, $url );
        }
        return $url;
    }
    add_filter( 'post_type_link', 'append_query_string', 10, 2 );