
برای اینکه یک پیوند یکتای سفارشی برای محصولات ووکامرس داشته باشیم می تونیم از توابع وردپرس برای این کار استفاده کنیم. توی بعضی از فروشگاه ها شاید نیاز باشه که آی دی محصول و همینطور دسته بندی محصول در پیوند یکتا وجود داشته باشه . برای این کار ما باید از طریق تابع آی دی محصول و نامک یا slug دسته بندی محصولات رو به پیوند یکتا اضافه کنیم .
add_filter('post_type_link', 'product_type_permalink', 10, 4); function product_type_permalink($post_link, $post, $leavename, $sample) { //If is custom post type "product" if ($post->post_type == 'product') { // Get current post object global $post; $post_id=$post->ID; $term_obj_list = get_the_terms( $post_id, 'city_categories' ); $ad_city=$term_obj_list[0]->slug; $p_cat = get_the_terms( $post_id, 'product_cat' ); $ad_cat=$p_cat[0]->slug; $permalink = get_bloginfo('url').'/'.$ad_city.'/'.$ad_cat.'/'.$post_id; } return $permalink; } add_action( 'init', 'wpse33551_rewrites_init' ); function wpse33551_rewrites_init(){ add_rewrite_rule( '([^/]+)/([^/]+)/([0-9]+)?$', 'index.php?post_type=product&p=$matches[3]', 'top'); }