WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Using Page Template for Category (2 posts)

  1. Doodlebee
    Member
    Posted 3 years ago #

    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?

  2. Doodlebee
    Member
    Posted 3 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.