WordPress as CMS and PHP

By Lewis Wolfe, Wednesday, July 9th, 2008

If you’re using WordPress as a CMS and doing some extra coding - for example picking something from an extra db table to display on a certain page, eg page_id=4

$pageVar = $wpdb->get_var(SELECT var FROM my_table WHERE something=’something_else’);

and then you also want to SEO the <title> a little, so in header.php you put:-

<?php if (is_page(’4′)) $extraTitle = ‘ยป ‘ . $pageVar; ?>

<title><?php bloginfo(’name’); ?> <?php wp_title(); ?> <?php echo $extraTitle; ?></title>

It won’t work - $pageVar doesn’t have global scope, because of the method used to get the header on the page.

<?php get_header(); ?>

Instead, one way round this is to use:

<?php include(TEMPLATEPATH . ‘/header.php’); ?>

and it will work
This has the nature of a hack - use sparingly and probably not in themes for general release. And stay away from $_GLOBALS

No responses so far

Leave a comment

Want a gravatar?
gravatars
Go to gravatar.com and upload your preferred avatar.

Your comment *

RSS feed for comments on this post. TrackBack URI