Luke Batley
Luke Batley

Reputation: 2402

Setting content for multiple pages in wordpress

Hi What i'm trying to do is in the page.php file set up a bunch of if statements for each page for example if page == aboutus display the about us content and so on. at the moment no matter what page i'm on it's displaying everything in the page.php file i'm sure its just something with the way i have written it. does anyone know what i'm doing wrong?

heres what i have so far

<?php get_head(); ?>

<?php is_page($page); ?>

<?php is_page('about-us'); ?> {

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">About Us</span>     

                <div class="msghead">
                <?php query_posts('cat=7&showposts=1&orderby=date'); while (have_posts()) : the_post(); the_content(); 
                endwhile;?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

}

<?php is_page('classes'); ?> {

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">Classes</span>     

                <div class="msghead">
                <?php query_posts('cat=7&showposts=1&orderby=date'); while (have_posts()) : the_post(); the_content(); 
                endwhile;?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

}

Upvotes: 0

Views: 154

Answers (3)

David Gard
David Gard

Reputation: 12087

You are not actually checking the page, you are just running the is_page() function, then displaying the content.

Additionally, when you use The Loop, you should first check that there are posts to display, to avoid errors.

And just as a tip - if the only difference in the pages is the posts that are shown, you can set your arguments before the loop, that way you can cut out quite a lot of your code. Here is a link to a Pastebin with an example of that method, taking the below. Furthermore, if the template is page.php, then the Page that you are viewing is already in the Loop, so you can make use of that to show the Page title. This is untested, but should get you started.

<?php get_head(); ?>

<?php if(is_page('about-us')) : ?>

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">About Us</span>     

                <div class="msghead">
                <?php
                query_posts('cat=7&showposts=1&orderby=date');
                if(have_posts()) : while (have_posts()) : the_post();
                        the_content(); 
                    endwhile;
                endif;
                ?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

<?php elseif(is_page('classes')) : ?>

    <div id="container">
    <?php get_header(); ?>
    <?php get_banner(); ?>

        <div class=" center content" role="main">
            <div id="posts">
                <span class="title">Classes</span>     

                <div class="msghead">
                <?php
                query_posts('cat=7&showposts=1&orderby=date');
                if(have_posts()) : while (have_posts()) : the_post();
                        the_content(); 
                    endwhile;
                endif;
                ?>
                </div>



             </div>

                   <div id="sidebardiv">
                <?php get_sidebar(); ?> </div>
                <div class="clear"> </div>

          </div>

       </div>
    <?php get_footer(); ?>

    </div>

<?php endif; ?>

Upvotes: 2

Tom
Tom

Reputation: 1366

You're missing an if statement.

e.g.

<?php if( is_page('about-us') ): ?>
  <p>about-us page here</p>
<?php elseif( is_page('classes') ): ?>
  <p>classes page here</p>
<?php endif; ?>

A more straightforward way is to create new templates for page-about-us.php and page-classes.php

Upvotes: 1

RRikesh
RRikesh

Reputation: 14411

An easier approach will be to use is_page('about-us') to check your current page.

Codex: http://codex.wordpress.org/Function_Reference/is_page

Upvotes: 2

Related Questions