2012-02-20 108 views
0

我創建我的自定義帖子類型,所有工作都很好,但是我只是有一個關於如何讓它顯示的簡單問題。使用創建WordPress的帖子類型

我在我的fuctions.php文件中創建了我的自定義帖子類型,並創建了單個speakers.php文件。如何將我的自定義發佈類型音箱頁面指向我的主導航欄中的「SAFETY SPEAKERS」選項卡?

THIS IS MY SITE

This is the actual link to my custom post type page with my speakers on it

fuctions.php

add_action('init', 'create_my_post_types'); 

function create_my_post_types() { 
    register_post_type('speakers', 
     array(
      'labels' => array(
       'name' => __('Speakers'), 
       'singular_name' => __('Speakers') 
      ), 
      'public' => true, 
     ) 
    ); 
} 

單speakers.php

<?php get_template_part('header', '2');  // Header #2 (header-2.php) ?> 

<?php query_posts('post_type=speakers'); ?> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

      <div id="speakersBox" class="clearfix"><?php the_title('<h6>', '</h6>'); ?><br/><?php the_content(); ?></div> 
      <hr> 

    <?php endwhile; endif; ?> 


<?php get_template_part('footer', '2');  // Footer #2 (footer-2.php) ?> 

任何幫助和或建議表示讚賞!

回答

0

假設您有single-speakers.php模板來顯示單揚聲器帖子類型。通過檢查帖子添加template_redirect操作。把它放在你的functions.php文件中。即

add_action('template_redirect', 'redirect_to_speaker'); 
function redirect_to_speaker(){ 
    global $post; 
    if(is_single() && $post->post_type == 'speakers'){ 
     include (TEMPLATEPATH . '/single-speakers.php'); 
     exit; 
    } 
} 

或者,您可以使用名爲template_include的過濾器。其實用這一個:

add_filter('template_include', 'my_speaker_template'); 
function my_speaker_template($template){ 
     global $post; 
     if(is_single() && $post->post_type == 'speakers'){ 
      $template = get_template_directory() . '/single-speakers.php'; 
     } 
     return $template; 
} 

此外,有關模板一些很好的信息,請閱讀這篇文章:http://xazure.net/2011/06/tips-snippets/wordpress/changing-wordpress-template-with-template_include/

+0

我在代碼中添加了我的single-speakers.php文件到我的問題中。我試過你的代碼,但沒有正確關閉?這是說有一個關閉錯誤,我需要把它放在一個特定的位置? (它高於我在functions.php文件中創建自定義帖子類型動作)。 – kia4567 2012-02-20 20:51:21

+0

對不起,我忘了提及將這些代碼放在你的'functions.php'文件中。我編輯了代碼。現在試試。 – 2012-02-21 05:47:35

+0

我加了,但是我不太明白它應該做什麼。你能幫我解釋一下嗎? – kia4567 2012-02-21 08:37:40

0

好,好,我想出了一個辦法。我使用single-speakers.php文件中的信息(並將其稱爲page-speakers.php)創建了一個新的模板頁面,並將我的Safety Speakers頁面指向該新頁面。它很棒!

感謝您的幫助Naveed,無論如何我都非常感激。

相關問題