WordPress.org

Ready to get started?Download WordPress

Forums

PHP include + Template path. (7 posts)

  1. cron
    Member
    Posted 9 years ago #

    If I wanted to use the PHP include function to pull up a page in a template using the TEMPLATEPATH handle how would I do it?

    What I want to do is set my .htacess file so it refrences error 404 to error_404.php in my root directory. In there I would like to include something like <?php include (TEMPLATEPATH . '/header2.php'); ?> that just uses the 404 page for my template. Is this possible? If so, how would I do it?

    Thanks for your help,
    Cron

  2. rwatkins
    Member
    Posted 9 years ago #

    Hey,

    I think it would be possible although you'd have to include wp-blog-header.php in the PHP that you are using so that it can use all the WordPress functions in it (and be able to use TEMPLATEPATH).

    You should then be able to do what you want.

    Rob

  3. cron
    Member
    Posted 9 years ago #

    I tried that but I did not know what to edit, what to deleate and what to keep.

  4. rwatkins
    Member
    Posted 9 years ago #

    Hey,

    You should be starting over with a new script in your root directory (called error_404.php - it doesn't already exist does it?). In that you want to put something like :

    <?php /* we have to have this to make TEMPLATEPATH
    point to the correct location */
    require('wp-blog-header.php');
    /* load up header2.php from the current template directory */
    <?php include (TEMPLATEPATH . '/header2.php'); ?>
    /*
    ?>

    If you want to get that loaded up when a 404 error occurs, you need to modify your .htaccess in the root directory. You might want to look at the Apache documentation for the ErrorDocument command but you would have to add something like:

    ErrorDocument 404 /error_404.php

    If you are still confused don't hesitate to reply.

    Rob.

  5. cron
    Member
    Posted 9 years ago #

    Thanks for your reply. First of, just to help, one error with your code, you put <?php inside another <?php tag. Works fine if you take that out (and the redundant ?> as well). However, even with those modifications, when I run this script I just end up with a blank page.

    Just to help, the code in my 404.php (in the template folder) (not header2, made a mistake with that), the code I am refrenceing to with the include (TEMPLATEPATH . '/404.php'); comand is this:

    <?php get_header(); ?>

    <div id="content" class="narrowcolumn">
    <div class="post_content">
    <div class="post_header">
    <div class="post-headline"><font class="post-date">Error 404 - Not Found </font>
    </div>
    </div>
    <div class="post">
    <div class="entrytext">

    Welcome to the 404 Page. You may have typed an invalid address or have clicked an inactive link. Feel free to mail me
    if you have any questions or problems. In the mean time feel free to
    keep browsing using the navigation bar at the right. You may also wnat
    to explore the day's interesting links for some unusual and funny
    websites. Sorry for the inconvenience, I hope it does not happen again.<br><br>Go Back

    </div>
    </div>
    <div class="post_footer"></div>
    </div>

    </div>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

    Thanks for healping me get this far,
    Cron

  6. rwatkins
    Member
    Posted 9 years ago #

    Hey,

    Apologies for the code. That was seriously bad - I should have re-read it!

    With a 404.php inside my templates directory and the following inside my error_404.php

    <?php /* we have to have this to make TEMPLATEPATH
    point to the correct location */
    require('wp-blog-header.php');
    /* load up header2.php from the current template directory */
    include (TEMPLATEPATH . '/404.php');
    ?>

    (thought it would be better to re-paste something that actually works ;)) then I see the contents of 404.php should I go to the error_404.php script in my wordpress root directory.

    Just check that it wasn't my (really) shoddy code that made you go wrong ;).

    If that doesn't work... I have to admit I'm not completely sure. Try adding error_reporting(E_ALL); to the top of error_4o4.php and see if PHP gives you any errors.

  7. cron
    Member
    Posted 9 years ago #

    Works fine now, thank you for your help.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.