REMOVE BRAND SLUGS TO IMPROVE LINKS. - by OnlyCBD

This topic has 2 replies, 2 voices, and was last updated 4 months ago ago by Andrew Mitchell

  • Avatar: OnlyCBD
    OnlyCBD
    Participant
    August 21, 2024 at 09:58

    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
    August 23, 2024 at 12:34

    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

  • Viewing 2 results - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.Log in/Sign up

We're using our own and third-party cookies to improve your experience and our website. Keep on browsing to accept our cookie policy.