WordPress.org

Ready to get started?Download WordPress

Forums

multiple Pages in a single Page? (3 posts)

  1. ambienttraffic
    Member
    Posted 8 years ago #

    I'm working on creating a web site for a friend, and it's turned out much more complicated than i thought. I wanted to be able to control all of the content through WordPress so that he can manage it. You can see the HTML version I've built here.

    I've been working on the page.php template. So far, I thought that I could make a Page for each area (i.e., the bottom "bio" area is a Page called "Variety Bio" and is child of "Variety", the "documents" area is a Page called "Variety Docs"), and have it change depending on the main Page that's called (in this case it would be the "Variety" Page).

    Is there a variable that I can use throughout to refer to the main Page ("Variety", in this case)? I tried doing something like this, where i made a variable called $mainID equal to the_ID() farther up the page:

    <?php

    if ($mainID = 3) {
    get_a_post(17);
    }elseif ($mainID = 4) {
    get_a_post(20);
    } elseif ($mainID = 5) {
    get_a_post(22);
    } else{
    echo "this is broken";
    }
    the_content();
    ?>

    I'm using the plugin Get-A-Post. It just kept giving back the mainID as 3 and displaying post 17 (the Variety Bio), even when I changed to another Page. I'm pretty new to PHP and I'm not sure exactly how to handle variables. Any suggestions or alternatives would be greatly appreciated.

    Thanks,
    Tracie

  2. maerk
    Member
    Posted 8 years ago #

    I think the_ID() echoes the post ID, rather than return it.

    When you echo something in PHP, as I'm sure you know, you actually insert it into the contents of the page. When a function returns something, it just provides a value for PHP to use later.

    For example: the_time() in WP actually inserts the post's time into the page content. However, get_the_time() returns it for use in PHP. So to use get_the_time() to echo the time, you would have to go echo get_the_time();.

    Perhaps there is an equivalent function for the ID, something like get_the_ID(). In that case you would need $mainID = get_the_ID();.

    Hope that helps, if not at least you learnt something about PHP ;)

  3. ambienttraffic
    Member
    Posted 8 years ago #

    thanks maerk -i finally got a chance to work on it today, and your advice led me to conditional tags, and it worked! i'm using is_page() to check the page name, then I set my variable $mainID. Then later on in the page I use the second bit of code, which i cleaned up by using a switch statement.

    So the first bit is
    <?php $mainID = '';
    if(is_page('Variety')){
    $mainID = 'Variety';
    }elseif(is_page('Comedy')){
    $mainID = 'Comedy';
    }elseif(is_page('Acting')){
    $mainID = 'Acting';
    }

    ?>

    and the second bit is
    <?php
    switch($mainID){
    case 'Variety':
    get_a_post(17);
    break;
    case 'Comedy':
    get_a_post(20);
    break;
    case 'Acting':
    get_a_post(22);
    break;
    default:
    echo "this is broken";
    break;
    }

    the_content();
    ?>

    ooh i love switch statements. ;-)

    oh, one more thing for anyone who's reading this thread - the bit of code that I have in the original post would never work because it needs a double equal (==) in the if statement, not a single (=)!!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags