Pages

Showing posts with label taxonomy. Show all posts
Showing posts with label taxonomy. Show all posts

Tuesday, March 25, 2014

How to set a default taxonomy for custom post types?

Please write below code in functions.php :

add_action( 'save_post', 'set_default_category' );
function set_default_category( $post_id ) {
    // Get the terms
    $terms = wp_get_post_terms( $post_id, 'your_custom_taxonomy');
    // Only set default if no terms are set yet
    if (!$terms) {
        // Assign the default category
        $default_term = get_term_by('name', 'taxonomy_slug', 'your_custom_taxonomy');
        $taxonomy = 'your_custom_taxonomy';
        wp_set_post_terms( $post_id, $default_term->term_id, $taxonomy );
    }
}

Tuesday, December 10, 2013

How to change Custom Taxonomy Links?


function custom_term_link_url($content) {
    $current_path = 'live-topics';
    $new_path = 'live/topic';
    $content = str_replace($current_path, $new_path, $content);
    return $content;
}
add_filter('term_link', 'custom_term_link_url');

function add_query_vars_for_live_topics($aVars) {
    $aVars[] = "live-topics";    // represents the name of the product category as shown in the URL
    return $aVars;
}
add_filter('query_vars', 'add_query_vars_for_live_topics');

function add_rewrite_rules_live_topics($aRules) {
    $aNewRules = array('live/topic/([^/]+)/?$' => 'index.php?live-topics=$matches[1]');
    $aRules = $aNewRules + $aRules;
    return $aRules;
}
add_filter('rewrite_rules_array', 'add_rewrite_rules_live_topics');