Title: Using Page Template for Category
Last modified: August 19, 2016

---

# Using Page Template for Category

 *  Resolved [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-page-template-for-category/)
 * Hey all,
 * I have an unusual situation where I need to associate categories with Pages, 
   and put different stuff from both on a single Page. Normally, I’d do this by 
   ensuring that the category name is exactly the same as the Page name, but I don’t
   have that option this time.
 * So what I’m attempting to do right now is 1) place the Page name that you want
   associated with the category in the “category description” field; and 2) use 
   a bit of code to pull the information for that Page, and “combine” the two together,
   using the template set for the Page. (as an aside, it would be awesome if you
   could create reusable templates for categories like you can for Pages. And also
   if you could create custom meta boxes for categories like you can for Posts and
   pages. But that’s not relevant right now :))
 * Anyway, so what I’ve got so far works BUT I’m running into a fatal error (exceeded
   allowed memory, blah blah, which I kind of expected with what I’m doing) and 
   I was wondering if someone might help me with what route I should be taking to
   finish this up.
 * What I have so far is this:
 *     ```
       function find_template() {
       	global $wpdb, $post;
       	$where = where_are_we();
       	if(is_category()) {
       	  $desc = get_category($where)->description;
       	  $page = get_page_by_title($desc);
       	  $pageID = $page->ID;
       	  $template = get_post_meta($pageID, '_wp_page_template', true);
                 $templatepath = get_query_template('page', $template);
       	}
       	  //if ( $template = apply_filters( 'template_include', $templatepath ) )
       		//include( $template );
       	//return;
       }
       ```
   
 * “where_are_we()” is another function I wrote that pulls in the toplevel Page 
   or Category ID, based on where we are on the site.
 * The commented out section is what I lifted from the template-loader.php file,
   and it’s what’s crashing – which I kind of expected, since I’m in a weird loop
   where I’m trying to pull a theme from within a theme 😉
 * I’m kind of stuck on the last bit – trying to figure out how to code this to 
   use the Page template that I’ve found ($template gives me what template file 
   the Page I’m after is using), and apply it to the category listing page I’m on.
 * Think anyone can give me a nudge in the right direction?

Viewing 1 replies (of 1 total)

 *  Thread Starter [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * (@doodlebee)
 * [15 years, 1 month ago](https://wordpress.org/support/topic/using-page-template-for-category/#post-2006942)
 * Okay, I think I’ve got it sorted. I still need to work out some kinks with the
   layout, but it seems to be working – my associated Pages/categories are using
   the same template that’s set for the Page.
 * Anyway, the function I used to make it work:
 *     ```
       function find_template() {
       	global $wpdb, $post;
       	$where = where_are_we();
       	if(is_category()) {
       	  $desc = get_category($where)->description;
       	  $page = get_page_by_title($desc);
       	  $pageID = $page->ID;
       	  $template = get_post_meta($pageID, '_wp_page_template', true);
       	  $templatepath = get_query_template('page', $template);
       	}
       	return $template;
       }
       ```
   
 * then, at the very top of my index.php file:
 *     ```
       <?php $template = find_template();
               if ($template) {
       		  locate_template($template, true);
               } else {
               	get_header();
               	get_sidebar();  ?>
       ```
   
 * So far it’s working just fine. I just have some layout issues to address. And
   again, the “where_are_we()” function I used up there is a custom written function
   to check to see where we are, and grab the ID of the topmost parent/category.

Viewing 1 replies (of 1 total)

The topic ‘Using Page Template for Category’ is closed to new replies.

 * 1 reply
 * 1 participant
 * Last reply from: [Doodlebee](https://wordpress.org/support/users/doodlebee/)
 * Last activity: [15 years, 1 month ago](https://wordpress.org/support/topic/using-page-template-for-category/#post-2006942)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
