我正在制作一个单页网站。我有一个与导航挂钩的自定义循环,这样只有导航中的帖子和页面才会显示在循环中。我发现使用get\\u template\\u part不会返回任何模板。我将其分配给一个变量来检查它,结果为NULL。
  if (($locations = get_nav_menu_locations()) && $locations[\'primary\'] ) {
    $menu = wp_get_nav_menu_object( $locations[\'primary\'] );
    $menu_items = wp_get_nav_menu_items($menu->term_id);
    $pageID = array();
    foreach($menu_items as $item) {
        if($item->object == \'page\')
            $pageID[] = $item->object_id;
    }
    $args = array(
        \'post_status\' => \'publish\',
        \'post_type\' => array( \'post\',\'page\' ),
        \'order\' => \'ASC\',
        \'orderby\' => \'menu_order\',
        \'post__in\' => $pageID,
        \'posts_per_page\' => -1,
        \'paged\' => (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1
    );
     $home_query = new WP_Query($args);
}
        if ( $home_query->have_posts() ) : while ( $home_query->have_posts() ) : $home_query->the_post(); ?>
 <?php $template_name = get_post_meta( $post->ID, \'_wp_page_template\', true );
                 if( $template_name != \'default\' && $template_name != \'template-home.php\') : ?>
                 <li id="page_<?php echo str_replace(\' \',\'_\', strtolower(get_the_title())); ?>">
                    <?php //require($template_name); ?>
                        <!--template-home.php-->
                            <li id="page_<?php echo str_replace(\' \',\'_\',strtolower( get_the_title())); ?>">
 switch($template_name){
 case \'template-band.php\':
 get_template_part(\'template\', \'band.php\');
 break;
 case \'template-band.php\':
 get_template_part(\'template\', \'contact.php\');
break;
case \'template-band.php\':
get_template_part(\'template\', \'discography.php\');
break;
case \'template-band.php\':
get_template_part(\'template\', \'gallery.php\');
break;
case \'template-band.php\':
get_template_part(\'template\', \'news.php\');
break;
 case \'template-band.php\':
get_template_part(\'template\', \'scroll.php\');
break;
case \'template-band.php\':
get_template_part(\'template\', \'shows.php\');
break;
case \'template-band.php\':
get_template_part(\'template\', \'video.php\');
break;
                <?php }
                                ?>
                            </li>
        <?php endif;
            endwhile;
            endif;
        ?>
 我对WordPress的发现是,如果您尝试在首页加载页面,它基本上会忽略除visual editor中的文本之外的所有其他内容。模板和大多数其他格式都不会应用。我不知道的是,为什么?
 
                    最合适的回答,由SO网友:Nicolai Grossherr 整理而成
                    作为法典第页,共页get_template_part() 表示:
get_template_part 不返回值,如果找不到匹配的模板文件,也不会发出警告。
此外:
如果您想了解有关失败的信息,请使用:
<?php assert( "locate_template( array(\'$name-$slug.php\', \'$name.php\'), true, false )" ); ?>
难怪你什么都没得到。
我看到的一件事是,您正在将文件扩展名添加到$name 参数这实际上意味着您正在查找名为:slug-name.php.php, 我怀疑这是你想要的。文件名的构造如下{$slug}-{$name}.php, 也就是说get_template_part() 自动添加文件扩展名,您可以通过查看source, 除此之外,它在法典页面上有明确的说明。