From 5304745be052aafe9ac44e4a86f490e24e778722 Mon Sep 17 00:00:00 2001 From: Eric Andrew Lewis Date: Fri, 8 Jan 2016 22:00:48 +0000 Subject: [PATCH] Permalinks: Make `get_post_type_archive_link()` work for the 'post' post type. Props jjj. See #19902. git-svn-id: https://develop.svn.wordpress.org/trunk@36225 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/link-template.php | 13 +++++++++ .../tests/link/getPostTypeArchiveLink.php | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/phpunit/tests/link/getPostTypeArchiveLink.php diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index b580a0a8a2..0b8354a92b 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -1124,6 +1124,19 @@ function get_post_type_archive_link( $post_type ) { if ( ! $post_type_obj = get_post_type_object( $post_type ) ) 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; diff --git a/tests/phpunit/tests/link/getPostTypeArchiveLink.php b/tests/phpunit/tests/link/getPostTypeArchiveLink.php new file mode 100644 index 0000000000..bb15c16a69 --- /dev/null +++ b/tests/phpunit/tests/link/getPostTypeArchiveLink.php @@ -0,0 +1,28 @@ +assertSame( $expected, $actual ); + } + + /** + * @ticket 19902 + */ + public function test_get_post_archive_link_with_post_archive_on_a_blog_page() { + $page_for_posts = $this->factory->post->create( array( 'post_title' => 'blog-page', 'post_type' => 'page' ) ); + update_option( 'show_on_front', 'page' ); + update_option( 'page_for_posts', $page_for_posts ); + $actual = get_post_type_archive_link( 'post' ); + $expected = get_permalink( $page_for_posts ); + $this->assertSame( $expected, $actual ); + } +}