0

I am using a custom query to get pages. I need them all displayed on the front page for a one page WordPress theme. For the most part, I have the theme working and things are fine. I have this problem with the code where it doesn't recognize page templates and other custom stuff like shortcodes. For the life of me, I can't figure out why. Some shortcodes work and others do not. I had to make a query that checks to see if the page has a page template asssigned and then run the template part for content-page. Here is my code for my loop

    $page_query = new WP_Query(array(
    'post_status' => 'publish',
    'post_type' => 'page',
    'order' => 'ASC',
    'orderby' => 'menu_order',
    'posts_per_page' => '-1',
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
));

        if ( $page_query->have_posts() ) : while ( $page_query->have_posts() ) : $page_query->the_post(); ?>
        <?php $template = get_post_meta( get_the_ID(), '_wp_page_template', TRUE ); ?>
        <?php if( $template && $post->post_content != '' ) { ?>
                    <li id="page_<?php echo str_replace(' ','_',strtolower( get_the_title())); ?>">
                        <?php get_template_part( 'content', 'page' ); ?>
                    </li>
         <?php } 

        endwhile;
        endif;
        ?>

I have to load all my content by a shortcode. If I try to set a custom page template to a page then when I load that page, it will be blank. The page ignores the content of the custom template. The loop does notice that a page template was assigned though.

what I would like to know is if there is a better way to load all pages into the front page and make WordPress recognize all content like it does for a regular page. I have been trying for months to come up with another way to no avail.

Thank you for your help.

  • I think maybe you're misunderstanding how templates work. they don't load within the loop, a page template is applied to the entire page, not to content within a page. – Milo Jan 14 '14 at 23:46
  • right. I figured that the reason you could query the post_type of page is so that you could get the content of that page. I would have thought that the page template would be part of that. Leave out the header and footer in the template and it should be good. when you query the post_type of page, it returns only the text within that page and nothing else. So my problem still remains. – Jamie Jan 15 '14 at 1:11
1

If your goal is to load the selected custom template for each page within the loop, then you're actually pretty close to that. You're getting the value of _wp_page_template, but then you're not doing anything with that template, you're just loading content-page.php for each of those pages with the line get_template_part( 'content', 'page' );.

If you instead take _wp_page_template and pass that value to get_template_part (minus the .php extension), then you'll get the behavior you describe.

if ( $page_query->have_posts() ) :
    while ( $page_query->have_posts() ) :
        $page_query->the_post();
        $template = get_post_meta( get_the_ID(), '_wp_page_template', true );
        if( $template && $post->post_content != '' ) {
            get_template_part( substr( $template, 0, -4 ) );
         }
    endwhile;
endif;
  • I tried it. Didn't work out. It was a nice idea. I may have made a mistake somewhere that made it fail. I had to move a lot of code to try it out. I think I will just stick with the method I am using. It's working for now. I am sure I will stumble upon a better method at a later date and come out with version 2.0. Thank you for the help Milo. – Jamie Jan 16 '14 at 2:30

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.