Skip to content

Instantly share code, notes, and snippets.

@chrisjdavis
Created December 10, 2012 22:22
Show Gist options
  • Save chrisjdavis/4253899 to your computer and use it in GitHub Desktop.
Save chrisjdavis/4253899 to your computer and use it in GitHub Desktop.
Completed functions file
<?php
function is_subcat( $term ) {
global $wpdb;
$check = $wpdb->get_var( "SELECT terms_id FROM " . $wpdb->prefix . "termsmeta WHERE meta_value = '" . $term . "' AND meta_key = 'non-unique-slug'" );
if( $check == NULL ) {
return false;
} else {
return true;
}
}
function is_special( $cat ) {
global $wpdb;
$slug = $wpdb->get_col( "SELECT meta_value FROM " . $wpdb->prefix . "termsmeta WHERE terms_id = " . $cat->term_id . " AND meta_key = 'non-unique-slug'" );
if( !empty($slug) ) {
return true;
} else {
return false;
}
}
function special_cat_link($cat) {
global $wpdb;
$slug = $wpdb->get_col( "SELECT meta_value FROM " . $wpdb->prefix . "termsmeta WHERE terms_id = " . $cat->term_id . " AND meta_key = 'non-unique-slug'" );
$parent = $wpdb->get_col( "SELECT meta_value FROM " . $wpdb->prefix . "termsmeta WHERE terms_id = " . $cat->term_id . " AND meta_key = 'parent_cat'" );
echo get_bloginfo('home') . '/' . $parent[0] . '/' . $slug[0];
}
function find_parent($current) {
$cat = get_term_by( 'name', $current, 'category' );
$parent = get_term_by( 'id', $cat->parent, 'category' );
return $parent->name;
}
function enable_non_unique_subcats( $query ) {
global $wpdb, $parent, $child;
if( is_subcat($query->query_vars['attachment']) == true ) {
$bits = explode('/', $_SERVER['REQUEST_URI'] );
$c = array_pop( $bits );
$p = array_pop( $bits );
$parent = get_category_by_slug( $p );
$ch = $wpdb->get_results( "SELECT terms_id FROM " . $wpdb->prefix . "termsmeta WHERE meta_value = '" . $parent->slug . "' AND meta_key = 'parent'" );
foreach( $ch as $chld ) {
$candidates = $wpdb->get_results( "SELECT terms_id, meta_value FROM " . $wpdb->prefix . "termsmeta WHERE terms_id = " . $chld->terms_id . " AND meta_key = 'non-unique-slug'" );
foreach( $candidates as $candies ) {
if( $candies->meta_value == $c ) {
$child = get_category($candies->terms_id);
}
}
}
load_template( TEMPLATEPATH . '/subcat.php' );
}
}
add_action( 'pre_get_posts', 'enable_non_unique_subcats' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment