WordPress.org

Ready to get started?Download WordPress

Forums

Elevate Parent Category Template
Fix for WP 3.1 (20 posts)

  1. lancemonotone
    Member
    Posted 3 years ago #

    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/

  2. echoz
    Member
    Plugin Author

    Posted 3 years ago #

    pushed to svn.

    thanks :)

  3. lancemonotone
    Member
    Posted 3 years ago #

    Thanks echoz! Great plugin.

  4. lancemonotone
    Member
    Posted 3 years ago #

    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.

  5. echoz
    Member
    Plugin Author

    Posted 3 years ago #

    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

  6. Spenser5286
    Member
    Posted 3 years ago #

    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..

  7. Oliver Nielsen
    Member
    Posted 3 years ago #

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

  8. echoz
    Member
    Plugin Author

    Posted 3 years ago #

    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.

  9. lancemonotone
    Member
    Posted 3 years ago #

    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.

  10. Spenser5286
    Member
    Posted 3 years ago #

    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');
  11. lancemonotone
    Member
    Posted 3 years ago #

    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);
        }
  12. Spenser5286
    Member
    Posted 3 years ago #

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

    thanks for trying though! :D

  13. lancemonotone
    Member
    Posted 3 years ago #

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

  14. Spenser5286
    Member
    Posted 3 years ago #

    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!

  15. echoz
    Member
    Plugin Author

    Posted 3 years ago #

    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);
        }
  16. Ojster
    Member
    Posted 3 years ago #

    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...

  17. echoz
    Member
    Plugin Author

    Posted 3 years ago #

    finally had time to take a look at the code. just pushed a fix. Should work now.

  18. echoz
    Member
    Plugin Author

    Posted 3 years ago #

    thought i'd detail more about this fix while its being processed by the wordpress servers.

    wordpress changed the way they did things in 3.1 with regards to hunting for templates to load.

    with this version, they looked into the $category object more specifically, 2 properties of it, slug and term_id respectively.

    the fix now modifies these 2 properties if its a >= wordpress version of 3.1.

    if i had more time, i'd try to figure out a more universal fix with regards to custom templates as well. other than that. this is it for now.

  19. Spenser5286
    Member
    Posted 3 years ago #

    It works! Thanks for the help, your plugin is awesome! My site basically depends on it lol.

  20. Ojster
    Member
    Posted 3 years ago #

    The new version of plugin works now! Thanks!

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic