Title: WordPress variable (the_category) between template files
Last modified: August 20, 2016

---

# WordPress variable (the_category) between template files

 *  Resolved [Martin Black](https://wordpress.org/support/users/ownedbyhleb/)
 * (@ownedbyhleb)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/wordpress-variable-the_category-between-template-files/)
 * I have my wordpress theme template files split up into different columns. On 
   my category pages I want to grab the category slug and run it through custom 
   loops to get the content from the category in question. So essentially my category.
   php looks a bit like this (simplified):
 *     ```
       <?php if (have_posts()) : ?><!-- Start the Loop. -->
       <?php $thiscategory = get_the_category();
       	  $slug = $thiscategory[0]->category_nicename;  ?>
       	  <div id="divcolumns">
       			<div id="col-left" class="home"><?php get_template_part('category','leftcol'); ?></div>
       			<div id="col-center" class="cat"><?php get_template_part('category','centercol'); ?></div>
       	  </div>
       <?php endif; ?><!-- end the loop -->
       ```
   
 * And then within the content files I’m using query posts with the variable $slug.
   e.g. “category_name=$slug&posts_per_page=1”.
 * How can I pass the variable $slug between my template files? Or is there a better
   way of getting the current category within an included file?

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/wordpress-variable-the_category-between-template-files/#post-2308482)
 * If you want to pass variables from one template to another, the best way to approach
   the problem is to turn `$slug = $thiscategory[0]->category_nicename;` into a 
   function that you can call within any file.
 *  Thread Starter [Martin Black](https://wordpress.org/support/users/ownedbyhleb/)
 * (@ownedbyhleb)
 * [14 years, 8 months ago](https://wordpress.org/support/topic/wordpress-variable-the_category-between-template-files/#post-2308487)
 * Any guidance on turning it into a function? Would I use a filter on the function`
   get_the_category()`?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘WordPress variable (the_category) between template files’ is closed to
new replies.

## Tags

 * [category](https://wordpress.org/support/topic-tag/category/)
 * [template](https://wordpress.org/support/topic-tag/template/)
 * [variable](https://wordpress.org/support/topic-tag/variable/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [Martin Black](https://wordpress.org/support/users/ownedbyhleb/)
 * Last activity: [14 years, 8 months ago](https://wordpress.org/support/topic/wordpress-variable-the_category-between-template-files/#post-2308487)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
