get_post_type_archive_link

Retrieves the permalink for a post type archive.

Description

(string|false) get_post_type_archive_link( (string) $post_type ); 

Returns (string|false)

The post type archive permalink.

Parameters (1)

0. $post_type (string)
The post type.

Usage

  1. if ( !function_exists( 'get_post_type_archive_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // The post type. 
  5. $post_type = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_post_type_archive_link($post_type); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_post_type_archive_link( $post_type ) { 
  2. global $wp_rewrite; 
  3. if ( ! $post_type_obj = get_post_type_object( $post_type ) ) 
  4. return false; 
  5.  
  6. if ( 'post' === $post_type ) { 
  7. $show_on_front = get_option( 'show_on_front' ); 
  8. $page_for_posts = get_option( 'page_for_posts' ); 
  9.  
  10. if ( 'page' == $show_on_front && $page_for_posts ) { 
  11. $link = get_permalink( $page_for_posts ); 
  12. } else { 
  13. $link = get_home_url(); 
  14. /** This filter is documented in wp-includes/link-template.php */ 
  15. return apply_filters( 'post_type_archive_link', $link, $post_type ); 
  16.  
  17. if ( ! $post_type_obj->has_archive ) 
  18. return false; 
  19.  
  20. if ( get_option( 'permalink_structure' ) && is_array( $post_type_obj->rewrite ) ) { 
  21. $struct = ( true === $post_type_obj->has_archive ) ? $post_type_obj->rewrite['slug'] : $post_type_obj->has_archive; 
  22. if ( $post_type_obj->rewrite['with_front'] ) 
  23. $struct = $wp_rewrite->front . $struct; 
  24. else 
  25. $struct = $wp_rewrite->root . $struct; 
  26. $link = home_url( user_trailingslashit( $struct, 'post_type_archive' ) ); 
  27. } else { 
  28. $link = home_url( '?post_type=' . $post_type ); 
  29.  
  30. /** 
  31. * Filters the post type archive permalink. 
  32. * @since 3.1.0 
  33. * @param string $link The post type archive permalink. 
  34. * @param string $post_type Post type name. 
  35. */ 
  36. return apply_filters( 'post_type_archive_link', $link, $post_type );