WordPress.org

Ready to get started?Download WordPress

Forums

How to include one page in another (when not in menu) (4 posts)

  1. eharris
    Member
    Posted 5 years ago #

    I want to include one page in another. Actually, I've got a couple paragraphs of content that I want to include on multiple pages. It's updated roughly monthly, so I want to be able to edit it only in one place and have it appear in others.

    I've got it working, but ONLY if the "Include this page in user menus" option is enabled in the page I want to include. I don't want it in the menus, though. I want it to be publicly viewable, but basically hidden.

    First, I installed the Exec-PHP plugin so that I could put PHP code into a post. Then, I stole some code from the Function Reference/get_pages codex page, and modified it to include only the page I want. The code is:

    <?php
        $pages = get_pages('include=144');
        $count = 0;
        foreach($pages as $page)
        {
            $content = $page->post_content;
            if(!$content)
                continue;
            if($count >= 2)
                break;
            $count++;
            $content = apply_filters('the_content', $content);
        ?>
            <div class=\"entry\"><?php echo $content ?></div>
        <?php
        }
    ?>

    Now, I confess that I don't understand this code in its entirety (I'm sure that a lot of it is unnecessary for a single-page usage, but until I get it working, I didn't want to put too much effort into paring it down), but if I change it to "include=2" (the front page), it works just fine. I can change the number to any page that is on the "Pages" menu. I've got two pages not on the menu and one that's saved as a private page, and none of them display. I'm not surprised the private page won't display, but what do I have to do in order to get a public page that's not on the menu to display?

  2. MichaelH
    Member
    Posted 5 years ago #

    Might take a different approach:
    http://wordpress.org/support/topic/239338

  3. jonmilani
    Member
    Posted 4 years ago #

    I try to avoid plugins whenever possible. This is exactly what I needed. Thanks.

  4. revmitcz
    Member
    Posted 4 years ago #

    If you're just looking to include one page into another page, without a plugin, the above code will work but here's a shorter version of it :

    <?php
    $include = get_pages('include=120');
    $content = apply_filters('the_content',$include[0]->post_content);
    echo $content;
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic