Support » Plugin: Elevate Parent Category Template » [Plugin: Elevate Parent Category Template] Fix for WP 3.1

  • function epct_redirect() {
    	global $wp_query;
    
    	if (is_category()) {
    		$childcat = $wp_query->query_vars['cat'];
    		$parent = get_category_parent($childcat);
    
    		$category = get_category($childcat);
    
    		if ($parent != 0) {
    
    			// fix from marty@halfempty to deal with custom template.
    			if (!file_exists(STYLESHEETPATH . '/category-' . $category->slug . '.php')) {
    			  //fix for WP 3.1
    			  $parent = get_category($parent);
    			  $wp_query->queried_object->slug = $parent->slug;
    			}
    		}
    
    	}
    }

    http://wordpress.org/extend/plugins/elevate-parent-category-template/

Viewing 15 replies - 1 through 15 (of 19 total)
  • Plugin Author echoz

    (@echoz)

    pushed to svn.

    thanks 🙂

    Thanks echoz! Great plugin.

    Sorry, but it’s not quite right yet. I saw that you’re checking for version and applying my fix but it’s still broken. I haven’t had a chance to find out why.

    Plugin Author echoz

    (@echoz)

    Your fix works for 3.1 alone? If that’s the case I’d just apply yours and put the min requirement as 3.1

    Hey gents, did this end up working?

    I updated the plugin but it seems to still be not working after the 3.1 wordpress upgrade. Just trying to find out if it’s on my end or not..

    Current 1.0.3 fixed version didn’t work for me either, using WP 3.1, which in this case is a former WP MU install.

    I have tried disabling other plugins to see if it was due to a conflict with another plugin, but that does not seem to be the case.

    Any fix welcome, as I’ve had to duplicate the parent category template for all the sub-categories, which works of course, but is not very elegant;-)

    Plugin Author echoz

    (@echoz)

    hi guys,

    would really love to fix this but haven’t found the time beyond my other commitments. if anyone wants to take a crack at this, just let me know and i’d merge it to the trunk asap.

    My fix above will work on WP 3.1 only, it’s not backwards compatible because I didn’t have the time to do it right. echoz version has a version check but it’s not implemented correctly. If you can figure out what’s happening there, you’ll have it. In other words, if version >= 3.1, do my fix, if it’s lower, use the original code.

    Thanks for the quick response guys~

    Lance, I have WP 3.1 and I implemented your fix, but It still seems to not work. I picked through my code against yours, but there doesn’t seem to be anything wrong. I simply copied your code into the plugin’s php. Is there a different way to implement the fix?

    this is the code i have for reference..

    function get_category_title($node) {
    	global $wpdb;
    	$test = $wpdb->get_var("SELECT name FROM $wpdb->terms WHERE term_id=$node");
    	return $test;
    }
    
    function get_category_child() {
    	global $wp_query;
    	return $wp_query->query_vars['cat_child'];
    }
    
    function is_parent() {
    	global $wp_query;
    	if ((get_category_parent($wp_query->query_vars['cat']) == 0) && (empty($wp_query->query_vars['cat_child']))) {
    		return true;
    	} else {
    		return false;
    	}
    }
    
    function get_category_parent($node) {
    	$path = get_category_path($node);
    
    	if (empty($path)) {
    		return 0;
    	} else {
    		return $path[0];
    	}
    }
    
    function get_category_path($node) {
    	global $wpdb;
    
    	$parent = $wpdb->get_var("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id=$node");
    	$path = array();
    
    	if ($parent != 0) {
    		$path[] = $parent;
    
    		$path = array_merge(get_category_path($parent), $path);
    	} 
    
    	return $path;
    
    }
    
    function epct_redirect() {
    	global $wp_query;
    
    	if (is_category()) {
    		$childcat = $wp_query->query_vars['cat'];
    		$parent = get_category_parent($childcat);
    
    		$category = get_category($childcat);
    
    		if ($parent != 0) {
    
    			// fix from marty@halfempty to deal with custom template.
    			if (!file_exists(STYLESHEETPATH . '/category-' . $category->slug . '.php')) {
    			  //fix for WP 3.1
    			  $parent = get_category($parent);
    			  $wp_query->queried_object->slug = $parent->slug;
    			}
    		}
    
    	}
    //	print_r($wp_query->query_vars);
    }
    
    add_action('template_redirect', 'epct_redirect');

    The problem is that $wp_version is global but hasn’t been declared within the epct_redirect() function. The function should be as follows:

    function epct_redirect() {
          global $wp_query, $wp_version;
    
          if (is_category()) {
            $childcat = $wp_query->query_vars['cat'];
            $parent = get_category_parent($childcat);
    
            $category = get_category($childcat);
    
            if ($parent != 0) {
              $wp_query->query_vars['cat_child'] = $childcat;
    
              // fix from marty@halfempty to deal with custom template.
              if (!file_exists(STYLESHEETPATH . '/category-' . $category->slug . '.php')) {
                if (version_compare($wp_version, '3.1', '>=')) {
                  //fix for WP 3.1
                  $parent = get_category($parent);
                  $wp_query->queried_object->slug = $parent->slug;
    
                } else {
                  $wp_query->query_vars['cat'] = $parent;
    
                }
    
              }
            }
    
          }
    
          //	print_r($wp_query->query_vars);
        }

    It still just doesn’t work for me. My child categories are not using their parent template.

    thanks for trying though! 😀

    Try changing STYLESHEETPATH to TEMPLATEPATH. It’s a longshot but it might work.

    yea no luck, thanks though!

    I guess I’ll just keep my eyes open for more ideas, I wish i knew more about PHP, I’d try to help figure out what’s wrong!

    Plugin Author echoz

    (@echoz)

    Hey guys, try replacing it epct_redirect with this and lemme know if it works

    function epct_redirect() {
          global $wp_query, $wp_version;
    
          if (is_category()) {
            $childcat = $wp_query->query_vars['cat'];
            $parent = get_category_parent($childcat);
    
            $category = get_category($childcat);
    
            if ($parent != 0) {
              $wp_query->query_vars['cat_child'] = $childcat;
    
              // fix from marty@halfempty to deal with custom template.
                if (version_compare($wp_version, '3.1', '>=')) {
                  //fix for WP 3.1
                  $parent = get_category($parent);
                  $wp_query->queried_object->slug = $parent->slug;
    
                } else {
                  $wp_query->query_vars['cat'] = $parent;
    
                }
            }
    
          }
    
          //	print_r($wp_query->query_vars);
        }

    Hello echoz,

    I tried your code and still no success. My subcategory pages still do not pick up the parent category template… None of the above fixes work…

Viewing 15 replies - 1 through 15 (of 19 total)
  • The topic ‘[Plugin: Elevate Parent Category Template] Fix for WP 3.1’ is closed to new replies.