WordPress.org

Ready to get started?Download WordPress

Forums

Get page content (6 posts)

  1. rize
    Member
    Posted 2 years ago #

    This is the code i'm currently using and it works:

    <?php
    $id=512;
    $post = get_page($id);
    $content = apply_filters('the_content', $post->post_content);
    $content = str_replace(']]>', ']]>', $content);
    echo $content;
    ?>

    What I want to achieve is instead of putting that code i want to have something like this instead:
    <?php echo getPageContent(8); ?>

    where I just put the pageid and the other code in functions.php..

    My questions is how do I transfer the working code to 'functions.php'

    Thanks!

  2. vtxyzzy
    Member
    Posted 2 years ago #

    Your function should be something like this (UNTESTED):

    function getPageContent($id=0) {
    $content = '';
    $post = get_page($id);
    if ($post) {
       $content = apply_filters('the_content', $post->post_content);
       $content = str_replace(']]>', ']]>', $content);
    }
    return $content;
    }

    I don't understand the purpose of the str_replace - it looks like it replaces a string with itself - but I left it in.

  3. rize
    Member
    Posted 2 years ago #

    thanks it worked!!! I hope I'll learn how to do that!

    I'm not sure with the str_replace but this is the only code that when used displays the formatting and shortcodes, others just get the text content.

  4. vtxyzzy
    Member
    Posted 2 years ago #

    The call to apply_filters should take care of the shortcodes and formatting. You might try commenting out the str_replace to see if it makes a difference.

  5. rize
    Member
    Posted 2 years ago #

    oh i thought it was the str replace.. okay I'll try that. thanks again!

    do you mind telling me what this means:
    $id=0

    why is it 0?

  6. simplistik
    Member
    Posted 2 years ago #

    He's just giving $id a default value it's meant to be replaced and can help eliminate any PHP Notices that you may get if your server is set up incorrectly. However, considering there would never be a post w/ the value of 0, may as well leave it blank, so that the function will error out if you don't feed it a proper number, thus requiring you to input an id.

    function getPageContent($id)

    but either way works fine

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.