我通常通过网上搜索或无休止的实验来寻找答案。这次我真的很绝望。我读了很多相关的问题,但没有一个能解决我的问题。
我使用'get_template_part'在许多其他主题,它工作得很好。这一次我不知道是什么造成了差异。
在我的主题中有一个注册的自定义帖子类型事件。事件中帖子的url看起来像example.com/event/wine-tasting。问题是:example.com/event/返回404页面而不是排列所有事件帖子(loop-event.php)
这是我的archive.php:
<?php get_header(); ?>
<?php if ( have_posts() ) the_post();?>
<?php rewind_posts();?>
<?php $post_type = get_post_type();
get_template_part( 'loop', $post_type );?>
<?php get_footer(); ?>
这是我的loop-event.php:
<?php get_header(); ?>
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="container">
<?php the_title();?>
<?php the_content();?>
</div>
<?php endwhile; ?>
这是我如何注册自定义post类型'event':
function yd_create_post_type_event()
{
$labels = array(
'name' => __( 'Events','yd'),
'singular_name' => __( 'Event','yd' ),
'add_new' => __('Add New','yd'),
'add_new_item' => __('Add New Event','yd'),
'edit_item' => __('Edit Event','yd'),
'new_item' => __('New Event','yd'),
'view_item' => __('View Event','yd'),
'search_items' => __('Search Event','yd'),
'not_found' => __('No event found','yd'),
'not_found_in_trash' => __('No event found in Trash','yd'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'query_var' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 0,
'supports' => array('title','editor','excerpt')
);
register_post_type('event',$args);
}
register_taxonomy(
"event-category",
array("event"),
array(
"hierarchical" => true,
"label" => __( "Event Categories",'yd' ),
"singular_label" => __( "Event Category",'yd' ),
"rewrite" => array('slug' => 'event')
)
);
我真的很感激你的帮助。我什么都试过了,真希望是我犯了一些愚蠢的错误。但到目前为止,我还没弄明白。提前感谢! 当你在WordPress中添加一个新的自定义帖子类型时,你需要刷新永久链接。请转到:Settings > Permalinks
,点击Save Changes按钮(重复两次)。这应该能解决你的问题。
需要刷新WordPress生成的重写规则
创建如下函数:
function rewrite_flush() {
flush_rewrite_rules();
}
如果自定义的帖子类型在你的模板中使用:
add_action( 'after_switch_theme', 'rewrite_flush' );
如果自定义的帖子类型在你的插件中使用:
register_activation_hook( __FILE__, 'rewrite_flush' );
相反的是@Sunny Johal的回答