RGilkes
RGilkes

Reputation: 35

Wordpress and PHP

I have this code in my header.php for Wordpress to display a mini feed at the top of the page or not.

<?php if ( !$noHeader ) { include('feed.php'); } ?>

At the top of each page I set the $noHeader variable

$noHeader = true;
get_header(); 

For some reason, this doesn't work. What am I doing wrong?

Upvotes: 0

Views: 356

Answers (2)

nickohrn
nickohrn

Reputation: 2248

Like Stephen said the get_header function doesn't give you access to variables in the scope you were in when you called it. You can get around this by globalizing your variables before calling get_header.

<?php // In your theme file
global $noheader;
$noheader = true;
get_header();

<?php 
global $noheader; 
if(!$noheader) { 
    include(TEMPLATEPATH.'/feed.php'); 
}

This may seem messy, and it is, but there's no reason not to do it because WordPress uses global variables all over the place. As I said in a comment to Stephen, this is better than directly including the header.php file in case you ever want to use parent/child themes.

Upvotes: 2

Stephen
Stephen

Reputation: 18964

I've had this problem. The wordpress function get_header(); does not evaluate local variables from the parent file in your included header file. Change it to

// get_header(); //commented out for clarity of explanation
include 'header.php';

Honestly, there's no real reason that I've found to use get_header(); over an include, anyway.

You might as well do this too:

// get_sidebar();
include 'sidebar.php';

// get_footer();
include 'footer.php';

Upvotes: 2

Related Questions