2017-10-05 156 views
1

我創建了一個自定義帖子類型名稱狗食測驗作爲一個WordPress插件。帖子類型顯示,但是當我創建single-dog-food-quiz.php文件時,頁面不顯示任何內容。這裏是我的代碼:WordPress的單一自定義帖子類型頁面不工作

對於狗食品quiz.php:

function dog_food_quiz() { 
    register_post_type('dog-food-quizzes', 
     array(
     'labels' => array(
      'name' => 'Dog Food Quiz', 
      'singular_name' => 'Dog Food Quiz', 
      'add_new' => 'Add New', 
      'add_new_item' => 'Add New Dog Food Quiz', 
      'edit' => 'Edit', 
      'edit_item' => 'Edit Dog Food Quiz', 
      'new_item' => 'New Dog Food Quiz', 
      'view' => 'View', 
      'view_item' => 'View Dog Food Quiz', 
      'search_items' => 'Search Dog Food Quizzes', 
      'not_found' => 'No Dog Food Quizzes found', 
      'not_found_in_trash' => 'No Dog Food Quizzes found in Trash', 
      'parent' => 'Parent Dog Food Quiz' 
     ), 

     'public' => true, 
     'menu_position' => 15, 
     'supports' => array('title', 'editor', 'comments', 'thumbnail', 'custom-fields'), 
     'taxonomies' => array(''), 
     'menu_icon' => 'dashicons-testimonial', 
     'has_archive' => true 
    ) 
); 
} 

add_action('init', 'dog_food_quiz'); 

add_filter('template_include', 'include_template_function', 1); 

function include_template_function($template_path) { 
    if (get_post_type() == 'dog_food_quizzes') { 
     if (is_single()) { 
     // checks if the file exists in the theme first, 
     // otherwise serve the file from the plugin 
     if ($theme_file = locate_template(array ('single-dog-food-quiz.php'))) { 
      $template_path = $theme_file; 
     } else { 
      $template_path = plugin_dir_path(__FILE__) . '/single-dog-food-quiz.php'; 
     } 
    } 
} 
    return $template_path; 
} 

這裏是單狗食品quiz.php代碼:

<?php get_header(); ?> 
<?php while (have_posts()) : the_post(); ?> 
    <main id="site-main" class="main-page article-page" data-ad-slots="top,rightrail1,stickyrightrail2,bottom,oop"> 
     <div class="inner dog-food-quiz-top"> 
      <section id="content"> 
       <h1>Can my Dog Eat This?</h1> 
       <div class="dfq-hero"> 
        <img src="/img/dog-food-quiz/hero.jpg" alt="dog with 2 bowls"> 
       </div><!-- dog food quiz hero --> 
       <p class="dfq-intro">Think again before tossing your dog that last bit from your dinner plate. How well do you know which human foods are okay for your canine friend to eat?</p> 
       <span class="dfq-disclaimer">*Always consult your veterinarian to help you understand the best diet for your dog.</span> 
       <div class="progress-container"><progress value="0" max="100" id="progressBar" style="width: 0%"></progress></div> 
       <div class="food-module"> 
        <div class="food-box"> 
         <span class="food-title">Apples?</span> 
         <img src="/img/dog-food-quiz/redapple200.jpg" alt="apple"> 
         <span class="correct" style="display: none;">Correct!</span> 
         <span class="incorrect" style="display: none;">Incorrect!</span> 
        </div><!-- food box --> 
        <div class="food-description" style="display: none;"> 
         <span>Remove the seeds and stem - then you have a sweet treat</span> 
        </div><!-- food description --> 
        <a class="yes-answer correct">YES</a> 
        <a class="no-answer incorrect">NO</a> 
       </div><!-- food module -->   
       <div class="results-block" style="display:none;"> 
        <div class="score-card"> 
         <h3 class="results-title"></h3><!-- results-title --> 
         <span class="results-comment"></span><!-- results-comment --> 
         <img src="" class="results-image" alt=""> 
        </div><!-- score-card --> 
        <div class="results-description"> 
         <p class="mobile-results"></p> 
         <p class="tablet-results"></p> 
         <div class="addthis_inline_share_toolbox"></div> 
        </div><!-- results-description --> 
       </div><!-- results-block --> 

      <div class="ad callout"> 
       <span class="ad" id="dfp_bottom"></span> 
      </div> 
     <div class="grid m-2col"> 
      <h2 class="title"><small>Most Popular In</small> 
       Did You Know? 
      </h2> 
     </div> 
    </section> 
    <aside id="sidebar-right" class="sidebar no-tablet"> 
     <div id="sidebar-right-contents"> 
      <div class="ad desktop just-tablet-up" id="sidebar-right-ad-1"> 
       <span class="ad" id="dfp_rightrail1"></span> 
       </div> 
      </div> 
     </aside> 
    </div> 
    </main> 

    <?php endwhile; ?> 

    <?php get_footer(); ?> 

我錯過這裏有什麼? 我得到的只是頁眉和頁腳以及一個空白頁面。請幫忙。

回答

0

您可能想嘗試將模板重命名爲single-dog-food-quizzes.php(即,將single-與您註冊的自定義帖子類型的名稱dog-food-quizzes相加)。

另外值得一提的是,在註冊由多個單詞組成的自定義帖子類型時,最好使用下劃線而不是連字符。所以你的情況: 狗食品測驗(單數)

然後你寫單:

register_post_type('dog_food_quizzes')

+0

感謝您回答Jase。我按照您的建議重新命名了該文件,但仍未顯示。 – MikeL5799

+0

謝謝,我仍然沒有試過 – MikeL5799

0

這可能,如果你的名字你自定義後類型(CPT)這樣會更好dog-food-quiz.php文件在你的主題中。

For Custom Post Types, should the slug be singular or plural?

+0

我也不明白你爲什麼寫 add_filter('template_include','include_template_function',1); ...以及後面的代碼。 如果我是對的,你不需要這樣做,因爲WordPress自動執行此操作。 這是模板層次結構: https://developer.wordpress.org/themes/basics/template-hierarchy/ –

+0

謝謝。我更改了名稱,但仍然沒有運氣 – MikeL5799

+0

您是否也在'add_filter'後刪除了代碼? –

相關問題