dqgg25493 2017-10-05 15:38
浏览 51

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

I have created a custom post type name Dog Food Quiz as a WordPress plugin. The post type shows, but when I create the single-dog-food-quiz.php file, the page doesn't show anything. Here is my code:

For 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;
}

Here is the code for 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(); ?>

Am I missing something here? All I get is the header and footer and a blank page. Please help.

  • 写回答

2条回答 默认 最新

  • duanjiao6730 2017-10-05 15:50
    关注

    You may want to try renaming your template to single-dog-food-quizzes.php (i.e. appending single- with the name of the custom post type, dog-food-quizzes, that you registered).

    Also it’s worth mentioning that it’s good practice to use underscores and not hyphens when registering a custom post type made up of more than one word. So in your case:

    register_post_type( 'dog_food_quizzes' )

    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值