Custom post type archives. see #13818.

git-svn-id: http://svn.automattic.com/wordpress/trunk@15803 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
nacin 2010-10-14 10:39:47 +00:00
parent 390bcdb27f
commit eb8762d626
3 changed files with 57 additions and 10 deletions

View File

@ -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 )

View File

@ -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() {
return (bool) $this->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();

View File

@ -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 );
}
/**