0

I am using include 'filename.php' to load a php file into a template file, simply because the original PHP template file was getting huge and I wanted to split up the sections into different files for easy reference later. Include works great until I try to use a function inside the included file, then it stops working. I searched around the web and it seems like since the included file is run on the remote server (is that right?) it does not pick up my function from functions.php as if it were running normally.

What is the correct way to do this? It seems I shouldn't use include and maybe should be using something else?

Simplified code example:

HTML:

<div id="section">
    <?php include 'codesnippet.php'; ?>
</div>

codesnippet.php:

<ul>
    <li>
    <?php the_title(); ?>
    </li>
</ul>

When any function is inside the included file, I get the error that the stream cannot be opened when trying to include it. The same type of error you would get if you mis-typed the filename in the include function.

1

This was a tough one to figure out, but I finally got it to work. You have to use locate_template to build the url like so:

include(locate_template('codesnippet.php'));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.