views:

329

answers:

1

I need to create a variable that can be accessed throughout my WordPress Theme template files (index.php, header.php etc..). I know function definitions go inside the functions.php template file (in your theme path), but there's no such thing for variables.

For example I constantly need to retrieve Categories in my Theme, so I would like to have this accessible from anywhere in my theme:

$categories = get_categories(); /* get_categories() is a wordpress function */

This way I can simply access to allocated data, without having to re-allocate it every time I need to get my categories.

Unfortunately adding that piece of code in my functions.php file, doesn't work, neither does making the variable global.

A: 

Apparently global does the trick. The problem was that my variable $categories should have been redefined with a global in front of it, in each template I needed to use it.

Luca Matteis
Does anyone else know of a more intuitive solution than having to redefine it as global in every template file? I use this method in developing my theme and it looks... out of place.
Eddie Ringle