是否在术语链接中添加自定义帖子类型名称?

时间:2014-11-12 作者:kallookoo

是否可以在分类术语的链接中添加自定义帖子类型的名称,并删除分类术语的slug?

默认链接为:slug\\u分类法/term\\u名称

我喜欢:post\\u type\\u name/term\\u name

注意:我使用WPML插件!!使用类型插件生成自定义帖子类型和分类

谢谢

1 个回复
SO网友:rado

是的,这是可能的。

首先,应该创建与WPML兼容的新rewrite\\u规则。此版本在URL中添加分类slug。(post\\u类型/分类法/术语)

/**
 * 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;
}
其次,您必须重写生成的term\\u链接:

/**
 * 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,它就像一个符咒:)

如果希望URL中没有分类名称。您必须绝对使用术语slug定义URL片段。否则,它将与single post type rewrite和get 404冲突(分类法的post\\u type/slug的定义晚于single的post\\u type/slug,因此具有更高的优先级并将搜索分类法术语)

有关实施的更多信息,请参见此处:http://someweblog.com/wordpress-custom-taxonomy-with-same-slug-as-custom-post-type/

结束

相关推荐

Custom permalinks structure

我希望有这样的结构:www.mysite.com/2013 (必须显示2013年的所有职位)www.mysite.com/my-category/2013 (必须显示2013年和“我的类别”类别的所有帖子)www.mysite.com/my-category/my-tag/ (必须显示所有类别为“我的类别”和标记为“我的标记”的帖子)www.mysite.com/my-category/ (必须显示“我的类别”类别的所有帖子)www.mysite.com/my-