2017-10-05 155 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'后删除了代码? –

相关问题