2013-03-22 105 views
1

通常我会尽我所能通过搜索在线或无休止的实验来找到答案。这一次我真的很绝望。我已经阅读了许多相关的问题,但没有解决我的问题。Wordpress get_template_part不工作 - 返回404页面而不是loop-xxx.php

我在许多其他主题中使用'get_template_part',它运行良好。这次我不知道是什么造成了差异。

在我的主题中有一个注册的自定义帖子类型事件。活动中帖子的网址看起来像example.com/event/wine-tasting

的问题是:example.com/event/返回一个404页面,而不是排队的所有事件的帖子(环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(); ?> 

这里是我的环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; ?> 

这是我如何注册自定义后类型“事件”:

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') 
    ) 
); 

我真的很感谢你的帮忙。我尝试过所有的事情,我希望是我犯了一些愚蠢的错误。但到目前为止我还无法解决这个问题。先谢谢你!

+1

此问题已排序......基本上,我认为* example.com/event/*将返回loop-event.php,但实际上它应该是类似url * example.com/event-category/all-事件/ *等...大脑短路。 – 2013-03-22 21:09:07

回答

4

当您在WordPress中添加新的自定义帖子类型时,您需要刷新/刷新永久链接。请转到:Settings > Permalinks,然后单击保存更改按钮(执行两次)。这应该可以解决你的问题。

+0

谢谢Sunny!我其实只是试过,它的工作,但只有在我创建了一个loop.php(没有* -event *)之后。因为我有其他的职位类型,我想使用loop-event.php和loop-lecture.php等。让我尝试添加一个重写到自定义帖子类型的注册...回到一分钟... – 2013-03-22 20:55:53

+0

我的荣幸颜,很高兴我能帮上忙。 – 2013-03-22 21:07:58

0

您需要刷新WordPress生成的重写规则。

创建如下功能:

如果自定义职位类型在模板中使用:

add_action('after_switch_theme', 'rewrite_flush'); 

如果自定义职位类型是你的插件使用:

register_activation_hook(__FILE__, 'rewrite_flush'); 

相反的是@Sunny Johal的回答。

相关问题