Custom post type archives. see #13818.
git-svn-id: https://develop.svn.wordpress.org/trunk@15803 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
2e864d29ad
commit
86ab32a196
|
@ -921,11 +921,28 @@ function register_post_type($post_type, $args = array()) {
|
|||
$args->rewrite['slug'] = $post_type;
|
||||
if ( !isset($args->rewrite['with_front']) )
|
||||
$args->rewrite['with_front'] = true;
|
||||
if ( !isset($args->rewrite['archive']) )
|
||||
$args->rewrite['archive'] = false;
|
||||
if ( !isset($args->rewrite['feeds']) || !$args->rewrite['archive'] )
|
||||
$args->rewrite['feeds'] = false;
|
||||
|
||||
if ( $args->hierarchical )
|
||||
$wp_rewrite->add_rewrite_tag("%$post_type%", '(.+?)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
|
||||
else
|
||||
$wp_rewrite->add_rewrite_tag("%$post_type%", '([^/]+)', $args->query_var ? "{$args->query_var}=" : "post_type=$post_type&name=");
|
||||
$wp_rewrite->add_permastruct($post_type, "{$args->rewrite['slug']}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
|
||||
|
||||
if ( $args->rewrite['archive'] ) {
|
||||
$archive_slug = $args->rewrite['archive'] === true ? $args->rewrite['slug'] : $args->rewrite['archive'];
|
||||
$wp_rewrite->add_rule( "{$archive_slug}/?$", "index.php?post_type=$post_type", 'top' );
|
||||
if ( $args->rewrite['feeds'] && $wp_rewrite->feeds ) {
|
||||
$feeds = '(' . trim( implode( '|', $wp_rewrite->feeds ) ) . ')';
|
||||
$wp_rewrite->add_rule( "{$archive_slug}/feed/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
|
||||
$wp_rewrite->add_rule( "{$archive_slug}/$feeds/?$", "index.php?post_type=$post_type" . '&feed=$matches[1]', 'top' );
|
||||
}
|
||||
$wp_rewrite->add_rule( "{$archive_slug}/page/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&paged=$matches[1]', 'top' );
|
||||
}
|
||||
|
||||
$wp_rewrite->add_permastruct($post_type, "{$archive_slug}/%$post_type%", $args->rewrite['with_front'], $args->permalink_epmask);
|
||||
}
|
||||
|
||||
if ( $args->register_meta_box_cb )
|
||||
|
|
|
@ -102,16 +102,22 @@ function wp_reset_postdata() {
|
|||
*
|
||||
* Month, Year, Category, Author, ...
|
||||
*
|
||||
* If the $post_types parameter is specified, this function will additionally
|
||||
* check if the query is for exactly one of the post types specified. If a plugin
|
||||
* is causing multiple post types to appear in the query, specifying a post type
|
||||
* will cause this check to return false.
|
||||
*
|
||||
* @see WP_Query::is_archive()
|
||||
* @since 1.5.0
|
||||
* @uses $wp_query
|
||||
*
|
||||
* @param mixed $post_types Optional. Post type or array of post types
|
||||
* @return bool
|
||||
*/
|
||||
function is_archive() {
|
||||
function is_archive( $post_types = '' ) {
|
||||
global $wp_query;
|
||||
|
||||
return $wp_query->is_archive();
|
||||
return $wp_query->is_archive( $post_types );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1297,11 +1303,13 @@ class WP_Query extends WP_Object_Query {
|
|||
$this->is_author = true;
|
||||
}
|
||||
|
||||
if ( '' != $qv['author_name'] ) {
|
||||
if ( '' != $qv['author_name'] )
|
||||
$this->is_author = true;
|
||||
}
|
||||
|
||||
if ( ($this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax) )
|
||||
if ( !empty( $qv['post_type'] ) )
|
||||
$this->is_archive = true;
|
||||
|
||||
if ( $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
|
||||
$this->is_archive = true;
|
||||
}
|
||||
|
||||
|
@ -2605,12 +2613,26 @@ class WP_Query extends WP_Object_Query {
|
|||
*
|
||||
* Month, Year, Category, Author, ...
|
||||
*
|
||||
* If the $post_types parameter is specified, this function will additionally
|
||||
* check if the query is for exactly one of the post types specified. If a plugin
|
||||
* is causing multiple post types to appear in the query, specifying a post type
|
||||
* will cause this check to return false.
|
||||
*
|
||||
* @since 3.1.0
|
||||
*
|
||||
* @param mixed $post_types Optional. Post type or array of post types
|
||||
* @return bool
|
||||
*/
|
||||
function is_archive() {
|
||||
function is_archive( $post_types ) {
|
||||
if ( empty( $post_types ) || !$this->is_archive )
|
||||
return (bool) $this->is_archive;
|
||||
|
||||
if ( ! isset( $this->posts[0] ) )
|
||||
return false;
|
||||
|
||||
$post = $this->posts[0];
|
||||
|
||||
return in_array( $post->post_type, (array) $post_types );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -3002,7 +3024,7 @@ class WP_Query extends WP_Object_Query {
|
|||
*/
|
||||
function is_singular( $post_types = '' ) {
|
||||
if ( empty( $post_types ) || !$this->is_singular )
|
||||
return $this->is_singular;
|
||||
return (bool) $this->is_singular;
|
||||
|
||||
$post_obj = $this->get_queried_object();
|
||||
|
||||
|
|
|
@ -781,7 +781,15 @@ function get_404_template() {
|
|||
* @return string
|
||||
*/
|
||||
function get_archive_template() {
|
||||
return get_query_template('archive');
|
||||
$post_type = get_query_var( 'post_type' );
|
||||
|
||||
$templates = array();
|
||||
|
||||
if ( $post_type )
|
||||
$templates[] = "archive-{$post_type}.php";
|
||||
$templates[] = 'archive.php';
|
||||
|
||||
return get_query_template( 'archive', $templates );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue