我使用以下代码注册了两个自定义帖子类型
add_action( 'init', 'create_post_types' );
function create_post_types() {
register_post_type( 'sector', array(
'labels' => array(
'name' => __( 'Sectors' ),
'singular_name' => __( 'Sector' )
),
'public' => true,
'has_archive' => true,
'supports' => array( 'title', 'editor' )
));
register_post_type( 'player', array(
'labels' => array(
'name' => __( 'Players' ),
'singular_name' => __( 'player' )
),
'public' => true,
'has_archive' => false,
'supports' => array( 'title', 'editor' )
));
}
这将正确创建Wordpress admin &我能够创建新的帖子。到目前为止一切顺利。当试图查看单个帖子或在存档中列出这些帖子时,问题就出现了。
在我的尝试中,我创建了archive-sector.php
和single-sector.php
…按照文档,当我访问如下URL时,我希望'single'文件中的代码显示:http://mydomain.com/sector/a-single-post/
但它没有,它只是从index.php
呈现代码。这很令人沮丧。谁能指出我错在哪里吗?
让我更困惑的是,我能够在我的front-page.php
中引用自定义WP_Query
中的帖子:
<?php
$args=array(
'post_type' => 'sector',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1
);
$get_sectors = new WP_Query($args);
if( $get_sectors->have_posts() ) {
while ($get_sectors->have_posts()) : $get_sectors->the_post();
?>
<div class="medium-6 columns">
<div class="panel">
<h3>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</h3>
</div>
</div>
<?php
endwhile;
}
wp_reset_query();
?>
显示文章标题没问题!
任何建议将是伟大的,因为我在这个茫然!
我以前遇到过同样的问题,解决方案是刷新重写规则。
这样做,进入Settings> Permalinks并保存您当前的设置。
这在Wordpress文档中有说明:http://codex.wordpress.org/Post_Types#Custom_Post_Type_Templates
我已经找到了解决方案(至少对我来说)…上面的代码似乎工作得很好。所需要做的就是在Wordpress内部重新保存永久链接(Settings > Permalinks
)的设置。
我只能假设这样做是为了清除缓存,因为/%category%/%postname%/
引用在注册自定义帖子类型之前已经插入了。