Instead of the project archives in my Divi theme being at /project_category/[category name]/ I want them to go to /portfolios/#[category name].
I modified the following code from this question and added it to my child theme’s functions.php file.
function my_category_redirect()
{
if ( is_category( ) ) {
$category = get_queried_object();
$cat_id = $category->term_id;
$url = site_url( '/portfolios/#' . $cat_id);
wp_safe_redirect( $url, 301 );
exit();
}
}
add_action( 'template_redirect', 'my_category_redirect' );
However this is not working. The project categories are still going to /project_category/[category name]/. I can’t tell if the my_category_redirect function is even being called.
Any help is much appreciated!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Your action callback probably gets called, but it could be that the if-statement that just fails. Are you using the default “category” taxonomy (i.e. Dashboard > Posts > Categories) or a custom taxonomy (i.e. Dashboard > {my-post-type} > {my-taxonomy}) as the project category?
If you’re using a custom taxonomy, then you should use is_tax(‘my-taxonomy’) instead of is_category() as the latter checks for the default category, not custom ones.
But, you can temporarily add a die('it works'); to your callback also, before the if statement, to verify that it gets called.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0