Support » Plugins » Including Other Files Based on Post ID

  • Resolved trivum


    Could someone help me out with the code for this? (I know almost nothing about writing PHP.)

    I would like to automatically include some pre-defined text from somewhere else every time a new post is created. The text would never be the same, however.

    There may be a simplier way to do this, but one way I thought of doing it was the following:

    Put the various pieces of different text in different files in a folder on my server. Name each of those files with number (e.g. 57.htm, 58.htm, 59.htm, etc.). And then with code, write an “include” php statement that would echo the post ID and insert that number into the include statement.

    For example: <?php include ‘http://www.mainsite/static-content/[echo-post-ID-number].htm’); ?>

    The result, it would seem to me, would be like this for Post #57:

    <?php include ‘http://www.mainsite/static-content/57.htm’); ?>

    In turn, this would then include the text that I have in my file 57.htm.

    Is there a way to echo the post ID in this way into an include statement? Or is there an easier way to do the whole thing?

    Thanks for any help.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Oh, nevermind. I’ve been looking all morning, and I just stumbled on the answer.

    <?php include(“http://www.mainsite/static-content/”.$post->ID.”.htm”) ?>

    Seems to work.

    According to PHP gurus you should never use URL in an include but server path…

    According to PHP gurus you should never use URL in an include but server path…

    Thanks for that, moshu. I’ve changed it.

    Also, I ran into another problem I hadn’t thought about, which is if the page doesn’t have a post ID – such as the home page or categories page, etc.

    So it looks like I found an answer to that. I also put the folder with the text files in my theme folder and so now I’m including them like this:


    if(is_single()) {
    echo “Some standard text.”;


    But now I have one more problem. If for some reason I don’t have enough text files to match the number of posts, how could I get that to output some other type of standard text or to just do nothing?

    For example, let’s say I have 224 text files, but then I hit Post #225. I get an error message because there is no 225.htm.

    Any suggestions? Thanks.

    OK, I think I’ve found the answer:

    Create a variable – in my case I named it mytext. Define the variable as the file that is to be included. In my case, I’ve put it in a folder called “extra” inside my theme folder.


    $mytext .= “extra/”.$post->ID.”.htm”;
    if (file_exists($mytext)) {
    } else {
    die (“Insert whatever text you want to appear here when either there is no post ID – such as on the homepage – or when there is no file corresponding to the post ID in the extra folder.”);


    It seems to be working. Anyone see any possible problems with it? I would appreciate the heads-up if you do.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Including Other Files Based on Post ID’ is closed to new replies.