Custom Post Type UI - Remove taxonomy slug from url


Очень долго боролся с проблемой, что при создании таксономии - ее slug потом появляется в урл при просмотре категорий. Для борьбы с проблемой даже установил плагин custom-post-type-permalinks, но он так ничем и не помог. И тут внезапно нашлось решение! Текст оригинала:

Lucky for you, I just had to do this for a client project. I used this answer on the WordPress Stackexchange as a guide:

/**
 * Tell WordPress how to interpret our project URL structure
 *
 * @param array $rules Existing rewrite rules
 * @return array
 */
function so23698827_add_rewrite_rules( $rules ) {
  $new = array();
  $new['projects/([^/]+)/(.+)/?$'] = 'index.php?cpt_project=$matches[2]';
  $new['projects/(.+)/?$'] = 'index.php?cpt_project_category=$matches[1]';

  return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

/**
 * Handle the '%project_category%' URL placeholder
 *
 * @param str $link The link to the post
 * @param WP_Post object $post The post object
 * @return str
 */
function so23698827_filter_post_type_link( $link, $post ) {
  if ( $post->post_type == 'cpt_project' ) {
    if ( $cats = get_the_terms( $post->ID, 'cpt_project_category' ) ) {
      $link = str_replace( '%project_category%', current( $cats )->slug, $link );
    }
  }
  return $link;
}
add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );

И вот тут важный момент - какие правки надо сделать в настройках самих кастомных постов и их таксономий в админке:

ПОЛЕЗНО  Очищаем HTML код от inline styles

When registering the custom post type and taxonomy, be sure to use the following settings:

// Used for registering cpt_project custom post type
$post_type_args = array(
  'rewrite' => array(
    'slug' => 'projects/%project_category%',
    'with_front' => true
  )
);

// Some of the args being passed to register_taxonomy() for 'cpt_project_category'
$taxonomy_args = array(
  'rewrite' => array(
    'slug' => 'projects',
    'with_front' => true
  )
);

Of course, be sure to flush rewrite rules when you're done. Good luck!


Итого: изначально я делал отдельные посты под галерею и ссылка на конечную страницу и категорию выглядела так:

http://site.com/gallery/3d-panorama/osennyaya-reka-samara-i-gorbatyiy-most-oktyabr-2014.html
http://site.com/gallery/gal_category/3d-panorama/

т.е. пост_тайп был gallery и таксономия под категории gal_category и с помощью кода выше удало урл категорий и страницы привести к таким:

http://site.com/gallery/3d-panorama/osennyaya-reka-samara-i-gorbatyiy-most-oktyabr-2014
http://site.com/gallery/3d-panorama/

при этом способе я потерял расширение .html в конце и отключил за ненадобностью плагин custom-post-type-permalinks