1

Is possible add name of custom post type in link of taxonomy term and remove slug of taxonomy?

The default link is: slug_taxonomy/term_name

I like: post_type_name/term_name

Note: I use WPML plugin!! with Types Plugin for generate custom post types and taxonomies

Thanks

1 Answer 1

1

Yes it is possible.

For first it should be created new rewrite_rules compatible with WPML. This version add taxonomy slug to URL. (post_type/taxonomy/term)

/**
 * Custom post type specific rewrite rules
 * @return wp_rewrite             Rewrite rules handled by Wordpress
 */
function cpt_rewrite_rules($wp_rewrite) {
  $rules = cpt_generate_archives('your_post_type_name', $wp_rewrite);
  $wp_rewrite->rules = $rules + $wp_rewrite->rules;
  return $wp_rewrite;
}
add_action('generate_rewrite_rules', 'cpt_rewrite_rules');

/**
 * Generate archive rewrite rules for a given custom post type
 * @param  string $cpt        slug of the custom post type
 * @return rules              returns a set of rewrite rules for Wordpress to handle
 */
function cpt_generate_archives($cpt, $wp_rewrite) {
  $rules = array();

  $post_type = get_post_type_object($cpt);
  if ($post_type->has_archive === false)
    return $rules;

  $languages = icl_get_languages();
  $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');

  $permalink_structs = array();

  foreach ($taxonomies as $key => $tax) {
    // get first associated taxonomy
    if ($tax->object_type[0] == $cpt) {
      $permalink_structs[] = array(
        'rule' => "{$tax->rewrite['slug']}/([^/]+)",
        'vars' => array($tax->query_var)
      );
      break;
    }
  }

  // generate for all languages
  foreach ($languages as $lang) {
    $slug_archive = WPML_Slug_Translation::get_translated_slug($post_type->rewrite['slug'], $lang['language_code']);

    foreach ($permalink_structs as $data) {
      $query = 'index.php?post_type='.$cpt;
      $rule = $slug_archive.'/'.$data['rule'];

      $i = 1;
      foreach ($data['vars'] as $var) {
          $query.= '&'.$var.'='.$wp_rewrite->preg_index($i);
          $i++;
      }

      $rules[$rule."/?$"] = $query;

      if ($post_type->rewrite['feeds']) {
        $rules[$rule."/feed/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
        $rules[$rule."/(feed|rdf|rss|rss2|atom)/?$"] = $query."&feed=".$wp_rewrite->preg_index($i);
      }
      if ($post_type->rewrite['pages']) {
        $rules[$rule."/page/([0-9]{1,})/?$"] = $query."&paged=".$wp_rewrite->preg_index($i);
      }
    }
  }
  return $rules;
}

For second, you must rewrite generated term_link:

/**
 * Rewrite taxonomy term link to custom term link
 * @param  string $url        The term URL
 * @param  string $term       The term object
 * @param  string $taxonomy   The taxonomy slug
 * @return url                returns term link url
 */
function rewrite_taxonomy_term_permalink($termlink, $term, $taxonomy) {
  if ('your_taxonomy_name' == $taxonomy) {
    $post_type = get_post_type_object('your_custom_post_type_name');
    $taxonomy_object = get_taxonomy($taxonomy);
    $slug_archive = WPML_Slug_Translation::get_translated_slug($post_type->rewrite['slug'], ICL_LANGUAGE_CODE);
    return home_url() . $slug_archive . '/' . $taxonomy_object->rewrite['slug'] . '/' . $term->slug . '/';
  }

  return $termlink;
}
add_filter( 'term_link', 'rewrite_taxonomy_term_permalink', 10, 3);

Vuala, it works like a charm :)

If you want without taxonomy name in URL. You must absolutely define URL fragment with term slug. Otherwise it will be in conflict with single post type rewrite and get 404 (post_type/slug for taxonomy is defined later than post_type/slug for single, so has higher priority and will search for taxonomy terms)

More about implementation here: http://someweblog.com/wordpress-custom-taxonomy-with-same-slug-as-custom-post-type/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.