Category in job permalink - Must save permalinks after adding the code to functions.php
function job_listing_post_type_link( $permalink, $post ) { | |
// Abort if post is not a job | |
if ( $post->post_type !== 'job_listing' ) | |
return $permalink; | |
// Abort early if the placeholder rewrite tag isn't in the generated URL | |
if ( false === strpos( $permalink, '%' ) ) | |
return $permalink; | |
// Get the custom taxonomy terms in use by this post | |
$terms = wp_get_post_terms( $post->ID, 'job_listing_category', array( 'orderby' => 'parent', 'order' => 'ASC' ) ); | |
if ( empty( $terms ) ) { | |
// If no terms are assigned to this post, use a string instead (can't leave the placeholder there) | |
$job_listing_category = _x( 'uncat', 'slug' ); | |
} else { | |
// Replace the placeholder rewrite tag with the first term's slug | |
$first_term = array_shift( $terms ); | |
$job_listing_category = $first_term->slug; | |
} | |
$find = array( | |
'%category%' | |
); | |
$replace = array( | |
$job_listing_category | |
); | |
$replace = array_map( 'sanitize_title', $replace ); | |
$permalink = str_replace( $find, $replace, $permalink ); | |
return $permalink; | |
} | |
add_filter( 'post_type_link', 'job_listing_post_type_link', 10, 2 ); | |
function change_job_listing_slug( $args ) { | |
$args['rewrite']['slug'] = 'job/%category%'; | |
return $args; | |
} | |
add_filter( 'register_post_type_job_listing', 'change_job_listing_slug' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Hi ! I have a problem ... I use your code for showing type in url and it work when i write the url directly in the navigator with the category but the links who are generated (for exemple in a list or directlty in the profil of the job) dosn't show the category and return an error 404. . .
It generate this type of link : "job//name-of-job" and no "job/category/name-of-job" ... I don't know what to do i search for 2 days now and i lost almost of time with that ...
I'm sorry for my english and i hope you understand me.