You must ensure that the post type is registered with has_archive
property set to true
看看
codex example, 将“书籍”替换为“产品”
function codex_custom_init() {
$labels = array(
\'name\' => \'Books\', \'singular_name\' => \'Book\',
\'add_new\' => \'Add New\', \'add_new_item\' => \'Add New Book\',
\'edit_item\' => \'Edit Book\', \'new_item\' => \'New Book\',
\'all_items\' => \'All Books\', \'view_item\' => \'View Book\',
\'search_items\' => \'Search Books\', \'not_found\' => \'No books found\',
\'not_found_in_trash\' => \'No books found in Trash\', \'parent_item_colon\' => \'\',
\'menu_name\' => \'Books\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'show_in_menu\' => true,
\'query_var\' => true,
\'rewrite\' => array( \'slug\' => \'book\' ),
\'capability_type\' => \'post\',
\'has_archive\' => true, // <- this arg must be true
\'hierarchical\' => false,
\'menu_position\' => null,
\'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\' )
);
register_post_type( \'book\', $args );
}
add_action( \'init\', \'codex_custom_init\' );
如果确定“has\\u archive”为true,请在主题根目录中创建一个名为
products.php
应该是这样的:
<?php get_header(); ?>
<div id="primary">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<div style="float: right; margin: 10px">
<?php the_post_thumbnail( array( 100, 100 ) ); ?>
</div>
<strong>Name: </strong><?php the_title(); ?><br />
</header>
<div class="entry-content"><?php the_content(); ?></div>
</article>
<?php endwhile; ?>
</div>
</div>
<?php get_footer(); ?>
然后创建一个名为
archive-products.php
简单地说:
get_template_part(\'products\');
然后创建一个名为
single-products.php
简单地说:
// yes, exactly the same thing
get_template_part(\'products\');