get_post_type_archive_link()
云策文档标注
概述
get_post_type_archive_link() 函数用于获取指定文章类型的归档页面固定链接。它接受一个文章类型参数,返回链接字符串或 false(如果文章类型不存在或无归档)。
关键要点
- 参数 $post_type 为必需字符串,指定文章类型名称。
- 返回值为字符串(归档链接)或 false(文章类型不存在或无归档)。
- 支持 'post' 文章类型的特殊处理,基于 'show_on_front' 和 'page_for_posts' 选项。
- 使用 get_post_type_object() 检查文章类型对象,并验证 has_archive 属性。
- 根据 'permalink_structure' 选项生成链接:有固定链接时使用重写规则,否则使用查询字符串。
- 提供 'post_type_archive_link' 过滤器钩子,允许自定义链接。
代码示例
$archive_link = get_post_type_archive_link( 'book' );
if ( $archive_link ) {
echo 'Archive Link: Books Archive';
} else {
echo 'The "book" post type does not have an archive.';
}注意事项
- 注册自定义文章类型时,需设置 has_archive => true 以确保此函数正常工作。
- 函数自 WordPress 3.1.0 引入,4.5.0 版本添加了对 'post' 文章类型的支持。
原文内容
Retrieves the permalink for a post type archive.
Parameters
$post_typestringrequired-
Post type.
Source
function get_post_type_archive_link( $post_type ) {
global $wp_rewrite;
$post_type_obj = get_post_type_object( $post_type );
if ( ! $post_type_obj ) {
return false;
}
if ( 'post' === $post_type ) {
$show_on_front = get_option( 'show_on_front' );
$page_for_posts = get_option( 'page_for_posts' );
if ( 'page' === $show_on_front && $page_for_posts ) {
$link = get_permalink( $page_for_posts );
} else {
$link = get_home_url();
}
/** This filter is documented in wp-includes/link-template.php */
return apply_filters( 'post_type_archive_link', $link, $post_type );
}
if ( ! $post_type_obj->has_archive ) {
return false;
}
if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) {
$struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive;
if ( $post_type_obj->rewrite['with_front'] ) {
$struct = $wp_rewrite->front . $struct;
} else {
$struct = $wp_rewrite->root . $struct;
}
$link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) );
} else {
$link = home_url( '?post_type=' . $post_type );
}
/**
* Filters the post type archive permalink.
*
* @since 3.1.0
*
* @param string $link The post type archive permalink.
* @param string $post_type Post type name.
*/
return apply_filters( 'post_type_archive_link', $link, $post_type );
}
Hooks
- apply_filters( ‘post_type_archive_link’, string $link, string $post_type )
-
Filters the post type archive permalink.
Skip to note 4 content
nilshenrikj
In the event that
get_post_type_archive_link()does not work or returns false, double check if you have included the argumenthas_archive => truewhen registering your post type.Skip to note 5 content
Codex
Example
<a href="<?php echo get_post_type_archive_link( 'movies' ); ?>">Movies Archive</a>Skip to note 6 content
Darshit Rajyaguru
This is the example for WordPress function that returns the URL of the archive page for a specific post type. It is used to retrieve the permalink for the post type’s archive. (For example, get the archive link for post type is “Book”)
$archive_link = get_post_type_archive_link( 'book' ); if ( $archive_link ) { echo 'Archive Link: <a href="' . esc_url( $archive_link ) . '" rel="nofollow ugc">Books Archive</a>'; } else { echo 'The "book" post type does not have an archive.'; }