Support » Fixing WordPress » Is there an easy way to fetch the body from a specific post?

  • Hi, this is my first post. I’ve been using various versions of WordPress for about two years now, without problems, but I’ve got a problem at the moment that I can’t work out.

    My website is distractionware.com, and the blog is in the subdirectory /blog/. The front page of my site has a one sentence summary of what’s currently happening on the site and a link to the different parts of it. I update this summary everytime I change something on the site, like a new blog post.

    I’m currently doing something a little weird here 🙂 I’m using phpBB forums and an extension for that called phpfetchall. It takes the content of a specific post on my forums and posts the text into the index.php on my frontpage.

    I’d like to get rid of that whole system by creating a page in wordpress and using some PHP functions to grab the text from that post. Is this difficult to do?

    For bonus points: Is there anyway to return the date and title of the most recent blog post? 🙂

Viewing 15 replies - 1 through 15 (of 20 total)
  • I’d like to get rid of that whole system by creating a page in wordpress and using some PHP functions to grab the text from that post. Is this difficult to do?

    Maybe use the RSS feed? I know you can do it that way, but I don’t know how. Many people do it though. I’m sure if you search, you can find info on it.

    Is there anyway to return the date and title of the most recent blog post? 🙂

    Return it? What do you mean? On the blog itself, or somewhere else?

    Wow… ok, thanks, but it looks like I haven’t explained myself properly at all. Let me try again.

    Totally separate from my blog, I’ve got an index.php file that fetches text from a specific phpBB forum post. I would like to replace this a little piece of php code to instead grab some text from a specific page on my blog. Is this easily doable, or am I better off coming up with a different solution?

    And additionally, is it possible to return, I dunno, a string containing the date and title of the most recent blog post to the same index.php file?

    Thanks in advance.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    I would like to replace this a little piece of php code to instead grab some text from a specific page on my blog.

    In your index.php file (not wordpress’s, the other one), add something along these lines:

    require_once('./blog/wp-blog-header.php');
    $post = get_post(POST_OR_PAGE_ID_NUMBER_HERE);
    $postcontent = apply_filters('the_content',$post->post_content);

    And additionally, is it possible to return, I dunno, a string containing the date and title of the most recent blog post to the same index.php file?

    require_once('./blog/wp-blog-header.php');
    if (have_posts())
    {
    the_post();
    $posttime = get_the_time('F jS, Y');
    $posttitle = get_the_title();
    }

    Obviously, you can format the post time however you please using PHP date specifiers. See here: http://php.net/date

    Thanks Otto, but I’m afraid I must be doing something wrong – that doesn’t seem to produce anything. Maybe I’m implementing it wrong? I don’t actually know the first thing about php…

    Here’s what the body of my frontpage looks like at the moment:

    [edit -it’s a bit messy, so I’m going to clean it up a bit.]

    <body background="images/back1.gif">
    
    <table height="100%" valign="middle" align="center">
      <tr><td>
        <center>
    
      <table width="80%" height="60" valign="middle" cellpadding="2" cellspacing="0">
      <tr><td width="175">
        <table bgcolor="#000000" width="100%" height="100%" cellspacing="1">
        <tr><td>
          <table bgcolor="#FFFFFF" width="100%" height="100%" cellspacing="1">
          <tr><td>
            //menustuff
          </td></tr></table>
        </td></tr></table>
      </td>
      <td width=10></td>
      <td>
        <table bgcolor="#000000" width="100%" height="100%" cellspacing="1">
        <tr><td>
          <table bgcolor="#FFFFFF" width="100%" height="100%" cellspacing="1">
          <tr><td>
            <center>
            <table width="90%" height="100%">
            <tr><td>
    
    //Ok, this is the bit:
    <?php require_once('./blog/wp-blog-header.php');
          $post = get_post(70);
          $postcontent =
             apply_filters('the_content',$post->post_content);
    ?>
    
    <?php require_once('./blog/wp-blog-header.php');
          if (have_posts())
          {
            the_post();
            $posttime = get_the_time('F jS, Y');
            $posttitle = get_the_title();
          }
    ?>
    
            </td></tr></table>
            </center>
          </td></tr></table>
        </td></tr></table>
      </td></tr></table>
    
        </center>
      </td></tr>
    </table>
    </center>
    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    You said you wanted strings. You didn’t say you wanted to just echo the stuff into the page itself. I thought you understood PHP.

    Use these instead:

    //Ok, this is the bit:
    <?php require_once('./blog/wp-blog-header.php');
          $post = get_post(70);
          echo apply_filters('the_content',$post->post_content);
    ?>
    
    <?php require_once('./blog/wp-blog-header.php');
          if (have_posts())
          {
            the_post();
            the_time('F jS, Y');
            the_title();
          }
    ?>

    Heh, no, sorry if I gave you that impression. I can just about manage to hack small changes on a theme 🙂 Php is on my “to learn” list at some distant point in the future.

    I’m afraid that doesn’t work either – no text is displayed…

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Huh. Works for me. Is “./blog/wp-blog-header.php” the correct path for your blog? That is, is your index.php file in a directory that also contains the “blog” subdirectory?

    Yep, the path is correct…

    I even tried using the full path,

    <?php require_once('http://www.distractionware.com/blog/wp-blog-header.php');
          $post = get_post(70);
          echo apply_filters('the_content',$post->post_content);
    ?>

    to be sure. I also tried messing around with that post number. It isn’t displaying anything…

    Hmm, ok, I’ve found an interesting bug that seems to be causing this. When I check the source of this newindex.php file, it displays the source, but it stops at the php queries. It doesn’t complete the tables, or display the </body> and </html> tags.

    Any ideas?

    And by the way, thanks for your help. I think we’re pretty close!

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Okay, you can’t use the “http” path. That certainly won’t work. It needs to be the actual path to the file on the server itself.

    The fact that it’s stopping at the php bits suggests that the path is wrong or that it doesn’t like that PHP for whatever reason. Try taking out the first chunk of PHP code and see if the second one works by itself.

    Yeah, that works. I guess the problem is with the first chunk of php code.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Huh. Try changing that last line to just:
    echo $post->post_content;

    And see if that works.

    I’m afraid not 🙁

    (bump) Sorry. Does anybody else have any ideas? Basically, the problem is to write a small bit of php code that will output the contents of a specific post. I don’t know php, so I don’t really know where to start.

    One last try, I think…

    So, any ideas? I haven’t managed to come up with an answer yet.

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘Is there an easy way to fetch the body from a specific post?’ is closed to new replies.