REMOVE BRAND SLUGS TO IMPROVE LINKS. - by OnlyBrand

1 reply2 participantsUpdated by Andrew Mitchell

  • Avatar: OnlyCBD
    OnlyBrand
    Participant

    Hello.
    We have a small problem with the Brands link, it is like this:
    https://localhost/brand/flores-cbd/ and we need it to be https://localhost/flores-cbd/ without brand.

    How could we do this?

    Thank you very much

    1 Answer

    Avatar: Andrew Mitchell
    Andrew Mitchell
    Support staff

    Hello, OnlyCBD,

    Thank you for reaching out to us. To remove the brand from the URL, you need to add the following code to the functions.php file of your child theme:

    // Remove /brand/ prefix from brand taxonomy URLs
    function custom_brand_rewrite_rules($flash = false) {
    	$taxonomy = 'brand';
    	$terms = get_terms($taxonomy, array('hide_empty' => false));
    
    	foreach ($terms as $term) {
    		add_rewrite_rule(
    			$term->slug . '/?$',
    			'index.php?' . $taxonomy . '=' . $term->slug,
    			'top'
    		);
    	}
    
    	if ($flash) {
    		flush_rewrite_rules(false);
    	}
    }
    add_action('init', 'custom_brand_rewrite_rules', 10, 0);
    
    function custom_brand_term_link($termlink, $term, $taxonomy) {
    	if ($taxonomy === 'brand') {
    		return home_url('/' . $term->slug . '/');
    	}
    
    	return $termlink;
    }
    add_filter('term_link', 'custom_brand_term_link', 10, 3);

    After that, go to Settings > Permalinks.
    Click the “Save Changes” button to flush the rewrite rules and apply the changes.

    If you need further assistance or have any questions, please feel free to contact us.

    Best Regards,
    8Theme’s Team

Want to reply? Log in, or register with your ThemeForest purchase code, to reply to this topic.

Login / Register