Wordpress单一自定义帖子类型页面不起作用

时间:2017-10-05 15:38:34

标签: php wordpress custom-post-type custom-wordpress-pages

我创建了一个自定义的帖子类型名称Dog Food Quiz作为WordPress插件。帖子类型显示,但是当我创建single-dog-food-quiz.php文件时,该页面没有显示任何内容。这是我的代码:

对于dog-food-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;
}

以下是single-dog-food-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(); ?>

我在这里遗漏了什么吗? 我得到的只是页眉和页脚以及一个空白页面。请帮忙。

2 个答案:

答案 0 :(得分:0)

您可以尝试将模板重命名为../styles/circle.css(即附加single-dog-food-quizzes.php,并附上您注册的自定义帖子类型single-的名称)。

另外值得一提的是,在注册由多个单词组成的自定义帖子类型时,最好使用下划线而不是连字符。所以在你的情况下:

dog-food-quizzes

答案 1 :(得分:0)

如果您将自定义帖子类型(CPT)命名为以下内​​容可能会更好: 狗食测验(单数)

然后你在主题中写下了single-dog-food-quiz.php文件。

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