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.

4 s
4

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.

Leave a Reply

Your email address will not be published. Required fields are marked *