2

I'm creating a theme for my portfolio site. After a some time of trial and error research and almostss, I'm stuck.

It's going to be very minimal site in content, therefore I decided to create a one page website. I'm using get_pages($args); to display each page in my front-page.php file.

This works great. Now here's where I'm stuck...I'd like to use a page to display my blog posts, which would be included in my one page flow and be called in my WP_Query same as the other pages.

So far I've tried the most documented method I can find; which is to change the page that's used to display posts in Settings>Reading.

To confirm

I have a static home page (front-page.php) which I've made a page template declared it in a page called home. Enabling me to set it in the reading settings, and I've created a page called blog for the same reason(this has template set to defualt).

enter image description here

My posts are not being loaded into the blog page set here.

I've read that a custom query could be interfering but I've checked this and it isn't the case in my situation.

Updated to use WP_Query rather get get_pages

front-page.php

    <?php

        $args = array (
            'post_type' => 'page',
            'order' => 'ASC',
            'orderby' => 'menu_order',
            'posts_per_page' => 5,
        );

        $query = new WP_Query( $args );

        if ( $query->have_posts() ) {
            while ( $query->have_posts() ) {
                $query->the_post(); ?>

                <a data-magellan-destination="<?php echo $post->post_name; ?>" name='<?php echo $post->post_name; ?>'></a>
                <div id="<?php echo $post->post_name; ?>" class="page-wrap full">
                    <h2><?php the_title() ?></h2>
                        <?php the_content() ?>
                </div>

            <?php }
        } else {
    }

        wp_reset_postdata();
    ?>

Has anyone come across this type of situation before? Or am I missing something with my loop that would prevent the blog post from loading. I'm learning php and WordPress and hit my limit of experience. I think the call to the content of the blog page itself is preventing the post information to come through? I'm not sure. Any guidance would be appreciated.

Thanks in advance

  • We can't help you with your code if we can't see it, can you edit your question and add it in? – Tom J Nowell Aug 23 '14 at 20:20
6

This doesn't work the way you think it does because get_pages doesn't do what you think it does.

First, understand that all pages, all content, in WordPress is really a "post". A "Page" is just a special type of post.

Now, in a normal environment, you wouldn't call "get_" anything. This is why you're confused, because you're directly getting things and thus bypassing everything that WordPress does automatically for you. When you call get_pages, then it gets those pages directly. It doesn't take the "static home page" setting into account, it simply does what it is told and gets the content of those pages.

See, WordPress automatically parses the URL and determines what it is that should be displayed on that URL. This happens according to the rewrite system. The rewrite rules turn a "pretty" URL into a series of parameters for the WP_Query system. The WP_Query system pulls out the posts to be displayed, and saves them in a global $wp_query. The template system then determines which template to pull from the theme based on the various data in that global WP_Query, and then loads that template. The template implements The Loop, which displays the posts contained within the global $wp_query.

So, to sum up:

  • URL gets processed by rewrites
  • Rewrites creates query parameters
  • Query parameters used to determine posts to get
  • Template system examines query to load proper template
  • Proper template displays the posts from the query

Each step here happens all by itself. You don't really control it, it's just what WordPress does at start-up.

The "Static Home Page" setting modifies step three there, where the query is getting the "home page" it has a decision to either get the posts from the blog or to get some static page. Similarly, when you tell it to get a specific Page, if that page is set to be the blog, then the query will get the posts to display there instead. Everything else happens normally.

By doing the get_pages yourself, you're bypassing everything here and just saying to get these pages and display them, period. Nothing else in the system will impact that because you've bypassed it all.

If you want to display everything on a single page, then you need to have it get exactly what you want it to display and then display that. In short, "Static Home Page" doesn't make the Pages have different content, it just changes what content is chosen by the query to display on those particular pages. If you want to get the posts, then you need to call get_posts in some form, or use a custom WP_Query, or modify the main query, or something else. The get_pages() function cannot really retrieve blog posts. Well, it can, but there's no point since get_posts() does that instead.

  • Thank you so much for your detailed explanation, really appreciate it. Whilst researching this I did feel that I was being drawn to WP_Query. For my situation, could I use a query to display pages and create a custom query in page template that display's my recent posts? Again thanks for your explanation of how WordPress processes a query. – JohnMcCarthy Aug 24 '14 at 0:41
  • Upvoted and extending your answer :-) – Pieter Goosen Aug 24 '14 at 7:33
2

Before I start, I would recommend that you read: Creating a Static Front Page

OK, when you select a static front page, one of the following templates are used to display the front page

  • front-page.php

  • page.php

  • any other custom page template

This is the part that you already have working from what I can understand from your question

Now, the blog page. Wordpress treats the page set as blog page as your home page, and I think it here that you get stuck. Wordpress automatically looks for a template called home.php, and if that template is not found, Wordpress will use index.php. Your blog.php is simply ignored. As index.php and home.php is only set up for the post type post, you get nothing shown when you visit your blog page

You have to alter the main query before it is executed to include page as a post type in your blog page using pre_get_posts. Do not create a custom query to make changes to the home page or any archive page as they are troublesome, specially when it comes to pagination. You can simply just add the following to your functions.php. Take note, the is_home() check is used to only alter the main query on the home page, which is your blog page

function include_page_in_home( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'post_type', array( 'post', 'page' ) ); // Remove post if you only need page
    }
}
add_action( 'pre_get_posts', 'include_page_in_home' );

For more parameters that you can check out WP_Query as both uses the same parameters. Just make sure that you are using the default loop in home.php or index.php.

Just for info, use the following in your functions.php when developing, don't use it in a production site. This will print the current template being used in the head section of the page if you are not sure which template is being used by a specific page

add_action('wp_head', 'show_template');
function show_template() {
    global $template;
    print_r($template);
}

Just to extend on the answer by @Otto, you can use the following in your front-page.php to display your pages. This is just a copied version from the codex which you can tweak as needed

<?php

// The Query
$the_query = new WP_Query( 'posts_per_page=-1&post_type=page' );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Additional resources

EDIT

If you need to show the latest three posts from your blog on your front-page.php, then you can simply just do this with a custom query, something like this

<?php

// The Query
$the_query = new WP_Query( 'posts_per_page=3&order=DESC' );

// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
  • Thank you for further expanding my understanding of WordPress. Are you suggesting in your answer that my best option for what I'm trying to achieve is to display my required pages on the blog page home.php or index.php? Rather than trying to display my blog as a page in a list of pages? I have understood the individual elements you have gone through but still a little stuck on my particular situation. – JohnMcCarthy Aug 24 '14 at 13:11
  • I'm not 100% sure what you need to display in your blog page, but your blog page should be index.php or home.php and not blog.php. Have you tried my last piece of code in your front-page.php. What are you getting from that. – Pieter Goosen Aug 24 '14 at 13:22
  • Thanks Pieter, I'm aware of the template structure. In my question I explain that I am creating a one page site. Each section of the site is a page. I now know to/how to use WP_Query to get the pages. I also got from you that I can tell the main query to include pages on my home.php or index.php. I want the last section of my one page site to be my blog page displaying the 3 latest posts. I could create section on my front-page.php with a loop calling those posts but I want the navigation item and the section to be a page. I'm off to my production comp now, thanks again for your time. – JohnMcCarthy Aug 24 '14 at 13:42
  • Did you see my update. – Pieter Goosen Aug 24 '14 at 16:05
  • Thanks @Pieter yes I did. I updated my question with a revised loop that I am now using to get my pages. I still want to pull my blog posts in a page and have that in the loop. I'm testing some solutions now. – JohnMcCarthy Aug 24 '14 at 17:00
-1

Without seeing your front-page.php file, here is what I'd suggest if you're using a posts page of "Blog". Create a custom page template for your Blog page. This is done the same way as front-page.php except for 2 things...

  1. You'll need to put the following comment at the top of your blog.php file (if that's what you're gonna call it.

    /* Template Name: Blog */

  2. Edit the Blog PAGE in the dashboard and select Blog as the template.

Or you could just create a template file called page-blog.php and you won't have to worry about selecting a template file in the dashboard.

Then within your page template, run your post loop to grab whatever content you want and display it however you want. I don't know exactly what content you want or how you want it displayed or I'd scratch something out for you.

Again, that's my best answer after not seeing your front-page.php file. Let me know if that helps!

  • Hi, thanks for your time. I've run through your suggestions 1. and 2. I also tried the page-blog.php first as that's the way I work with templates... – JohnMcCarthy Aug 24 '14 at 0:25
  • The blog page does not use page templates, but home.php or index.php – Pieter Goosen Aug 24 '14 at 7:33

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.