2013-03-22 68 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的回答。

相關問題