Having the category/categories in the URLs
-
Hi there,
How would it be possible to have the categories of jobs in the URLs?
For example: website.com/category/sub-category/job-listing-title
I have tried the following PHP, but didn’t work:
function custom_job_listing_rewrite_rules() {
add_rewrite_rule(
'^jobs/([^/]+)/([^/]+)/?$',
'index.php?job_listing=$matches[2]&job_category=$matches[1]',
'top'
);
}
add_action('init', 'custom_job_listing_rewrite_rules');
function custom_job_listing_permalinks($post_link, $post) {
if ($post->post_type === 'job_listing') {
$terms = get_the_terms($post->ID, 'job_category');
if ($terms && !is_wp_error($terms)) {
$category_slug = $terms[0]->slug;
return home_url("/jobs/$category_slug/" . $post->post_name . '/');
}
}
return $post_link;
}
add_filter('post_type_link', 'custom_job_listing_permalinks', 10, 2);
function flush_custom_rewrite_rules() {
custom_job_listing_rewrite_rules();
flush_rewrite_rules();
}
register_activation_hook(FILE, 'flush_custom_rewrite_rules');
register_deactivation_hook(FILE, function () {
flush_rewrite_rules();
});
- You must be logged in to reply to this topic.