• Resolved Martin Black

    (@ownedbyhleb)


    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)
  • 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

    (@ownedbyhleb)

    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.