0

I'm creating a custom theme in Wordpress 4.1. When trying go to a page that is not the home or portfolio (which have their custom pages), it should load page.php. But instead I get the index.

No plugins cause a change in this, as I tried to disable all and then retry. No change.

My page.php is a very simple one, taking example from the twentyfifteen theme:

<?php get_header() ?>

<?php
    // Start the loop.
    while ( have_posts() ) : the_post();

        // Include the page content template.
        get_template_part( 'content', 'page' );

    // End the loop.
    endwhile;
?>

<?php get_footer() ?>

I read at places that I need to put if(have_posts()) before the loop, but there is none in the default theme, so I don't see why I'd need that.

I also copied the code from page.php into index.php, which display nothing except the header.php and footer.php pages.

I'm quite new to Wordpress as well, so that doesn't quite help either.

Could anyone tell me why the page doesn't load as I would want it to?

2

If you give a look to get_template_part it says that <?php get_template_part( 'loop', 'index' ); ?> will do a PHP require() for the first file that exists among these, in this priority:

  1. wp-content/themes/twentytenchild/loop-index.php
  2. wp-content/themes/twentyten/loop-index.php
  3. wp-content/themes/twentytenchild/loop.php
  4. wp-content/themes/twentyten/loop.php

so your code would default to content.php, not page.php.


<?php get_template_part('page') ?> should just load a partial named page.php and shouldn't be slow at all.

But the point is that if you are not using a custom template for the "basic" pages you shouldn't be using get_template_part inside the page.php at all. That page.php get's called when it's any single page that you haven't assigned a template to.

So try making your page.php like this

<?php get_header() ?>

<?php
    // Start the loop.
    while ( have_posts() ) : the_post();

        // Include the page content.
       the_content();

    // End the loop.
    endwhile;
?>

<?php get_footer() ?>
  • I don't quite understand what you mean. I looked at the page and into the code where the function is located. It implied to me I needed to do <?php get_template_part('page') ?> instead, which caused the loading speed to be incredibly slow. – MisterBla Mar 23 '15 at 0:42
  • Maybe I cleared my thoughts on the edit. – jimihenrik Mar 23 '15 at 0:48
  • Using the_content() worked. Thank you very much. :) – MisterBla Mar 23 '15 at 0:50

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.