'', 'show_option_none' => __('No categories'),
'orderby' => 'name', 'order' => 'ASC',
'show_last_update' => 0, 'style' => 'list',
'show_count' => 0, 'hide_empty' => 1,
'use_desc_for_title' => 1, 'child_of' => 0,
'feed' => '', 'feed_type' => '',
'feed_image' => '', 'exclude' => '',
'exclude_tree' => '', 'current_category' => 0,
'hierarchical' => true, 'title_li' => __( 'Categories' ),
'echo' => 1, 'depth' => 0,
'taxonomy' => 'category'
);
$r = wp_parse_args( $args, $defaults );
if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] )
$r['pad_counts'] = true;
if ( isset( $r['show_date'] ) )
$r['include_last_update_time'] = $r['show_date'];
if ( true == $r['hierarchical'] ) {
$r['exclude_tree'] = $r['exclude'];
$r['exclude'] = '';
}
if ( !isset( $r['class'] ) )
$r['class'] = ( 'category' == $r['taxonomy'] ) ? 'categories' : $r['taxonomy'];
extract( $r );
if ( !taxonomy_exists($taxonomy) )
return false;
$categories = get_categories( $r );
$output = '';
if ( $title_li && 'list' == $style )
$output = '
' . $title_li . '';
if ( empty( $categories ) ) {
if ( ! empty( $show_option_none ) ) {
if ( 'list' == $style )
$output .= '- ' . $show_option_none . '
';
else
$output .= $show_option_none;
}
} else {
if( !empty( $show_option_all ) )
if ( 'list' == $style )
$output .= '- ' . $show_option_all . '
';
else
$output .= '' . $show_option_all . '';
if ( empty( $r['current_category'] ) && ( is_category() || is_tax() || is_tag() ) ) {
$current_term_object = get_queried_object();
if ( $r['taxonomy'] == $current_term_object->taxonomy )
$r['current_category'] = get_queried_object_id();
}
if ( $hierarchical )
$depth = $r['depth'];
else
$depth = -1; // Flat.
$r['walker'] = new Walker_Category_Unlink_Parents( $r['taxonomy'] );
$output .= walk_category_tree( $categories, $depth, $r );
}
if ( $title_li && 'list' == $style )
$output .= '
';
return $output;
}
/**
* Create HTML list of categories, with unlinked parent categories.
* Based on Walker_Category class from wp-includes/category-template.php
*
* @uses Walker
*/
class Walker_Category_Unlink_Parents extends Walker {
var $tree_type = 'category';
var $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
// Define six new member variables to hold the option settings, and the list of all term ids.
var $option_dummy = 1;
var $option_unlink_current = 0;
var $option_remove_titles = 0;
var $option_unlink_specified_only = 0;
var $option_unlink_array = array();
var $all_terms = array();
// Define a constructor to load the option settings from the db, and initialize the term id list.
function Walker_Category_Unlink_Parents( $taxonomy = 'category' ) {
$unlink_options = get_option('ambrosite_unlink_parent_cats');
$this->option_dummy = is_null($unlink_options['dummy']) ? 1 : $unlink_options['dummy'];
$this->option_unlink_current = $unlink_options['unlink_current'];
$this->option_remove_titles = $unlink_options['remove_titles'];
$this->option_unlink_specified_only = $unlink_options['unlink_specified_only'];
if ( $unlink_options['excats'] )
$this->option_unlink_array = array_map( 'intval', explode(',', $unlink_options['excats']) );
$this->all_terms = get_terms( $taxonomy, array( 'hide_empty' => false, 'hierarchical' => true ) );
}
function start_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent\n";
}
function end_lvl(&$output, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$indent = str_repeat("\t", $depth);
$output .= "$indent
\n";
}
function start_el(&$output, $category, $depth, $args) {
extract($args);
$cat_name = esc_attr( $category->name );
$cat_name = apply_filters( 'list_cats', $cat_name, $category );
// Begin modified code
$unlink_current = false;
if ( $this->option_unlink_current && $category->term_id == $current_category )
$unlink_current = true;
$link_open = '';
if ( _get_term_children($category->term_id, $this->all_terms, $taxonomy) && !$this->option_unlink_specified_only || in_array($category->term_id, $this->option_unlink_array) || $unlink_current ) {
if ( $this->option_dummy ) {
$link_open = '';
}
}
$link = $link_open;
if ( !$this->option_remove_titles ) {
if ( $use_desc_for_title == 0 || empty($category->description) )
$link .= 'title="' . esc_attr( sprintf(__( 'View all posts filed under %s' ), $cat_name) ) . '"';
else
$link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
}
$link .= '>';
$link .= $cat_name . $link_close;
// End modified code
if ( !empty($feed_image) || !empty($feed) ) {
$link .= ' ';
if ( empty($feed_image) )
$link .= '(';
$link .= '';
$link .= '';
if ( empty($feed_image) )
$link .= ')';
}
if ( !empty($show_count) )
$link .= ' (' . intval($category->count) . ')';
if ( !empty($show_date) )
$link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
if ( 'list' == $args['style'] ) {
$output .= "\tterm_id;
if ( !empty($current_category) ) {
$_current_category = get_term( $current_category, $category->taxonomy );
if ( $category->term_id == $current_category )
$class .= ' current-cat';
elseif ( $category->term_id == $_current_category->parent )
$class .= ' current-cat-parent';
}
$output .= ' class="' . $class . '"';
$output .= ">$link\n";
} else {
$output .= "\t$link
\n";
}
}
function end_el(&$output, $page, $depth, $args) {
if ( 'list' != $args['style'] )
return;
$output .= "\n";
}
}
function ambrosite_unlink_parent_cats_admin() {
?>
Ambrosite Unlink Parent Categories - Options