Prevent category looping. Props tellyworth. fixes #7267

git-svn-id: http://svn.automattic.com/wordpress/trunk@8299 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2008-07-09 04:57:18 +00:00
parent 0cd103e2a2
commit 100ed8a62d
1 changed files with 8 additions and 5 deletions

View File

@ -1,6 +1,6 @@
<?php <?php
function get_category_children($id, $before = '/', $after = '') { function get_category_children($id, $before = '/', $after = '', $visited=array()) {
if ( 0 == $id ) if ( 0 == $id )
return ''; return '';
@ -14,7 +14,8 @@ function get_category_children($id, $before = '/', $after = '') {
$category = get_category($cat_id); $category = get_category($cat_id);
if ( is_wp_error( $category ) ) if ( is_wp_error( $category ) )
return $category; return $category;
if ( $category->parent == $id ) { if ( $category->parent == $id && !in_array($category->term_id, $visited) ) {
$visited[] = $category->term_id;
$chain .= $before.$category->term_id.$after; $chain .= $before.$category->term_id.$after;
$chain .= get_category_children($category->term_id, $before, $after); $chain .= get_category_children($category->term_id, $before, $after);
} }
@ -44,7 +45,7 @@ function get_category_link($category_id) {
return apply_filters('category_link', $catlink, $category_id); return apply_filters('category_link', $catlink, $category_id);
} }
function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE){ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename = FALSE, $visited = array()){
$chain = ''; $chain = '';
$parent = &get_category($id); $parent = &get_category($id);
if ( is_wp_error( $parent ) ) if ( is_wp_error( $parent ) )
@ -55,8 +56,10 @@ function get_category_parents($id, $link = FALSE, $separator = '/', $nicename =
else else
$name = $parent->cat_name; $name = $parent->cat_name;
if ( $parent->parent && ($parent->parent != $parent->term_id) ) if ( $parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited) ) {
$chain .= get_category_parents($parent->parent, $link, $separator, $nicename); $visited[] = $parent->parent;
$chain .= get_category_parents($parent->parent, $link, $separator, $nicename, $visited);
}
if ( $link ) if ( $link )
$chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator; $chain .= '<a href="' . get_category_link($parent->term_id) . '" title="' . sprintf(__("View all posts in %s"), $parent->cat_name) . '">'.$name.'</a>' . $separator;