WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm developing my own theme and came up with an issue that' I am confused with. I have two files that I'm trying to figure out. page.php and index.php.

I know that page.php is used for pages, of course, but when trying to set up a 404 page, for example:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

    <section class="main-content">
        <div class="content-wrap">
            <div class="module page">
                <h1 class="title"><?php wp_title(''); ?></h1>
                <?php the_content(); ?>
            </div><!-- /.blog -->
        <?php get_sidebar(); ?>
        </div><!-- /.content-wrap -->

<?php endwhile; else : ?>
404 not found
<?php endif; ?>

Nothing will be displayed. Now, if I copy this content and stick it in index.php, I will have a 404 page. I have no idea what to do because index.php is required for a theme but I don't know what to do with it. Can someone explain the differences and what kind of content actually goes in it?

share|improve this question

Index.php isn't for a specific page, and that's not how template files in themes work.

You will need to understand the template heirarchy


enter image description here

WordPress uses the Query String — information contained within each link on your web site — to decide which template or set of templates will be used to display the page.

First, WordPress matches every Query String to query types — i.e. it decides what type of page (a search page, a category page, the home page etc.) is being requested.

Templates are then chosen — and web page content is generated — in the order suggested by the WordPress Template hierarchy, depending upon what templates are available in a particular WordPress Theme.

WordPress looks for template files with specific names in the current Theme's directory and uses the first matching template file listed under the appropriate query section below.

With the exception of the basic index.php template file, Theme developers can choose whether they want to implement a particular template file or not. If WordPress cannot find a template file with a matching name, it skips down to the next file name in the hierarchy. If WordPress cannot find any matching template file, index.php (the Theme's home page template file) will be used.

share|improve this answer

Ian Stewart's WordPress THEME DEVELOPMENT TUTORIAL will surely help you the most.
It presents a nice theme for me.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.