190 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $1+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
ThemeBuddies says

I am trying to make a one page WordPress site and I have having some difficulty getting the pages to show on the homepage. I thought that I could just use the index file and not set up a custom page template for the homepage. I used pre_get_posts to get the pages to show on the front page but it ignores the templates. I shows the pages as a post and will not recognize that a page template is assigned.

does anyone have some good advice for setting up a one page WordPress site? I am attempting to get every post and page into an unordered list. I have had great success but also problems with my method as it duplicated posts. I just can’t nail down a good method for doing this. Do you have some advice you can share?

Thank you.

41 posts
  • Has collected 1+ items on Envato Market
  • Located in United Kingdom
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 1 year
Makins says

I don’t know how others do it, but I just use WP Query multiple times, instead of using the loop.

Each WordPress page needs to be a section of a single page website, so you can use a simple WP_Query to get the content and the title and display it as a section.

Something like this:
$query = new WP_Query( 'post_type=page' ); 
if ( $query ->have_posts() ) : while ( $query ->have_posts() ) : $query ->the_post(); 
     echo '<article class="section">'; 
     the_title( '<h1>', '</h1>' );
     the_content();
     echo '</article>'; 
endwhile; 
endif; 

Hope this helps!

190 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $1+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
ThemeBuddies says

that poses the same problem that I have been having though. You can’t get page templates to work on the front page with that code. The only way I have been able to do it so far is by using two queries on the homepage. One using a foreach loop with get_pages to get all page templates and include() the page-template into the homepage and another standard loop. Problem with this way is that it duplicates posts. Not only does it show the page template, but it also shows the page that was created that the template was assigned to.

What I am trying to do is get all posts, pages and templates into the front page so that I can put them into a unordered list so that I can slide to the next page.

41 posts
  • Has collected 1+ items on Envato Market
  • Located in United Kingdom
  • Sells items exclusively on Envato Market
  • Has been part of the Envato Community for over 1 year
Makins says

Oh I see what you’re trying to do, I had this problem with page templates. I don’t know if this is a very good method, but I used something like this:

$query = new WP_Query( 'post_type=page' ); 
if ( $query ->have_posts() ) : while ( $query ->have_posts() ) : $query ->the_post(); 
     if( get_post_meta( get_the_id(), '_wp_page_template', true ) == 'page-template.php' ) :
          //this is your page template code
          echo '<article class="template">'; 
          the_title( '<h1>', '</h1>' );
          the_content();
          echo '</article>'; 
     else: 
          echo '<article class="normal">'; 
          the_title( '<h1>', '</h1>' );
          the_content();
          echo '</article>'; 
     endif;
endwhile; 
endif;

Let me know if I’ve misunderstood again!

190 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $1+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
ThemeBuddies says

well, I understand what your code is doing but it’s not working for me. I like the approach though. I am going to do some reading in the codex and see if I can make it work. It is doing the same thing. Still not showing the template. It is getting the regular pages though.

I think it would work better to say

190 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $1+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
ThemeBuddies says

I think what it needs to say is

if is page template; include(the template name); else get all other pages

3436 posts
  • Has sold $5,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Located in United States
  • Has been part of the Envato Community for over 4 years
+1 more
organicbee says

I think what it needs to say is if is page template; include(the template name); else get all other pages
if ( is_page_template('supercoolcustompage.php') ) {

//do stuff

} else {

//do other stuffs
}
190 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $1+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
ThemeBuddies says

yea but I think there is another way than calling each template individually. I was calling them based on the _wp_page_template meta_key using get pages and it was working but I was also getting duplicates which is why I am hunting for another way. I need to get one page, not both.

741 posts
  • Had an item featured on Envato Market
  • Won a Most Wanted contest
  • Has been part of the Envato Community for over 5 years
  • Has sold $40,000+ on Envato Market
+4 more
Orbital_Themes says

What do you mean by getting duplicates? The _wp_page_template meta gives you the accurate file name that needs to be included. You can then do something like that:

$template = get_post_meta( $post->ID, '_wp_page_template', true );

get_template_part( $template );

EDIT: You would still probably need to do some more checks if, for example, a page doesn’t have any template assigned (the meta will return ‘default’).

190 posts
  • Has been part of the Envato Community for over 2 years
  • Has sold $1+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
ThemeBuddies says

I did do something like that using get pages. This worked to get all the page templates onto my homepage which I then organized into a unordered list so that I can slide the page into view


p $args = array( 'meta_key' => '_wp_page_template');
            $templates = get_pages($args);
            //var_dump($templates);
            foreach ( $templates as $template){
                $templateName = $template->meta_value;
                $pageName = $template->post_name;
                if( $templateName != 'default' && $templateName !='') { ?>
                <li id="page_<?php echo str_replace('-','_',$pageName); ?>"><?php
                    include($templateName);
                ?></li><?php
                }
            }

So that code above gets the page templates just great. But it does not get all the other pages. So I had to run another loop to get them. For that, I used pre_get_posts in functions.php to get the pages onto the homepage. so I ran into duplicates because the get_pages code would get the page template, and then the loop would get that same page without the template assigned.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by