0

I am trying to make a loop to loop all pages beneath each order. I want this because I want to make a one-pager with a page as an element of the one-pager. So that you can go into WP to edit a page and choose a template, and it will be printed through a loop.

It was going well until I thought of something.

I have a loop, but it displays the title and the content, so not the whole page. I want several pages with different page templates. So you can go in the dashboard, select the template and it will be printed differently (and also different custom fields are added each template).

My loop is this:

$args = array( 'post_type' => 'page', 'order' => 'ASC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="section" id="<?php echo $post->post_name; ?>" style="background-image: url('<?php the_field('backgroundimg_home'); ?>'); background-color: <?php the_field('achtergrondkleur'); ?>">
    <h1><?php the_title(); ?> </h1>
    <?php the_content(); ?>
    </div>
<?php endwhile; ?>

So i want to print each page beneath each other, I have no idea how.. Isn't there just something like get_the_page..?

I studied the WordPress codex but couldn't find anything.

  • I just found out it might be get_page_template. – Maartje Jan 2 '14 at 13:50
1

First of all, your page templates will normally include everything needed to make up a page, i.e. including this structure:

<html>
    <head></head>
    <body></body>
</html>

which is not what you want over and over in your one-pager.

However if you've built your templates using the get_template_part() function (described quite well over here) you can reuse it for the listing.

Just figure out which template part it is you want to use. You could use the get_page_template() function (or the get_page_template_slug() or even something like this:

global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );

which ever suits your needs) to identify which template is being used. Then you can use this information ^ to call the right get_template_part() for each loop.

^ You can do this by stripping it down to remove anything up until and including "/" and anything after and including "." to get the actual descriptive name of the template. Or by just comparing the return value you get with the different alternatives you have. Depends on what your file structure looks like and what method you're most comfortable with.

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.