• Hi,
    I am building a theme loosely following the structure of WordPress default theme. I completed index.php and started to split it up into different files.
    I cut and pasted everything in the page up to the ‘content’ div into a new file, and saved this as ‘header.php’ in the same directory as index.php. I used the code <?php get_header(); ?> to include it.
    However, this broke some of the php code, such as get_category_rss_link and get_permalink, which worked before.
    I tried changing get_header to <?php include(TEMPLATEPATH.'/header.php'); ?> and it worked.
    Have I made a mistake or does certain code break when using get_header? Also, do I need to use get_header for certain functions or will include() suffice?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter triplex2k7

    (@triplex2k7)

    Ok, this is to do with my last question about whether get_header is required for the theme to work.
    I’ve looked in wp-includes/general-template.php where function get_header is defined and it does the following things:

    1. do_action( 'get_header' );
    2. Checks if there is a header.php template file.
    3. If there is, it includes it
    4. If there isn’t, it includes one from the default theme

    As far as I know, do_action is used to create hooks for plugins to use. So, should I just add the code do_action( 'get_header' ); above my include(TEMPLATEPATH.'/header.php')?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    No, you should use the get_header() function instead of reinventing the wheel. The get_header() function is expected and should always be used. Same as with get_sidebar() and get_footer(). Just because that is what it does now does not mean that that is not subject to change in the future, and so you need to do the right thing in your theme to be forward-compatible.

    If this causes you problems, then you need to explain those problems in more detail, so that we can help you solve them. Use of get_header() should not change the get_permalink or get_category_rss_link functions, as those are not Loop functions and they use their own globals.

    However, use of get_header will remove access to global vars from the header.php file. This is intentional, if you want to use globals in header.php, you have to declare them as such.

    Thread Starter triplex2k7

    (@triplex2k7)

    Ok thanks. I think the problem is with global variables rather than the functions.

    Thread Starter triplex2k7

    (@triplex2k7)

    Thank you, that has solved it.

    The problem was that I was defining variables containing page and category IDs in functions.php (Is this bad practise? Is there a better place to put them?) and I did not know that you could not use global variables in header.php.

    To solve this I added the code <?php global $myvar1, $myvar2, $myvar3; ?>.

    This works, but I am worried that I may still be going about this the wrong way.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘get_header doesn’t work but include(TEMPLATEPATH.’/header.php’) does’ is closed to new replies.