1

I am making a one page site. I have a custom loop that is hooked to the navigation so that only the posts, pages that are in the navigation will show up in the loop. I have found that using get_template_part is not returning any templates. I assigned it to a variable to check it and got 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;
        ?>

What I have discovered about WordPress is that if you try to load pages on the front page, it basically will ignore every thing else except the text in visual editor. Templates, and most other formatting does not get applied. What I have not been able to figure out is, Why?

4

As the codex page of get_template_part() says:

get_template_part doesn't return a value and doesn't warn if it fails to find a matching template file.

Additionally:

If you want to hear about failures, use:
<?php assert( "locate_template( array('$name-$slug.php', '$name.php'), true, false )" ); ?>

So no wonder you are get nothing back.

One thing I'm seeing is that your are adding the file extension to the $name parameter. Which essentially does mean you are looking for files named: slug-name.php.php, I doubt that is what you want. The filenames are constructed like this {$slug}-{$name}.php, which means get_template_part() adds the file extension automatically, as you can discover by looking into the source, aside from that it is explicitly stated at the codex page.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.