Support » Fixing WordPress » Get page content

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

    $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’


Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.

    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.

    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.

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

    do you mind telling me what this means:

    why is it 0?

    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

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get page content’ is closed to new replies.