永久链接中的Wordpress自定义分类值,但如何创建404?

时间:2012-01-24 12:54:29

标签: php wordpress

我已经创建了自定义分类(品牌)和自定义帖子类型(产品)和产品,我创建了以下代码,重新编写永久链接,使它们显示为:

  

www.mysite.com/sony/w123-widescreentv

......这很棒。但是,如果我修改URL的品牌部分,它不会触发404,即:

  

www.mysite.com/sonyasdasdsad/w123-widescreentv

...仍然加载相同的页面 - 这很糟糕,因为从SEO的角度来看,它可能意味着无限量的重复内容。

我如何修改我的代码以检查网址中的品牌是否与帖子的品牌匹配,如果没有转到404页面?

function create_products() {
  register_taxonomy (
    'product_brand',
    array(),
    array (
      'hierarchical' => true,
      'labels' => array (
        'name' => _x('Brands', 'taxonomy general name'),
        'singular_name' => _x('Brand', 'taxonomy singular name')
      ),
      'show_ui' => true,
      'query_var' => 'brand',
      'rewrite' => array( 'slug' => 'brand' )
    )
  );
  register_post_type (
    'product',
    array (
      'taxonomies' => array('product_brand'),
      'labels' => array (
        'name' => _x('Products', 'post type general name'),
        'singular_name' => _x('Products', 'post type singular name')
      ),
      'supports' => array('title', 'editor', 'excerpt', 'custom-fields', 'thumbnail', 'page-attributes'),
      'public' => true,
      'has_archive' => 'full-price-list',
      'menu_icon' => null,
      'menu_position' => 20,
      'rewrite' => array('slug' => '%product_brand%')
    )
  );
}

// Permalink Re-writes
function custom_post_link($post_link, $id = 0) {
  $post = get_post($id);
  if (!is_object($post) || $post->post_type != 'product') {
    return $post_link;
  }
  $client = 'misc';
  if ($terms = wp_get_object_terms($post->ID, 'product_brand')) {
    $client = $terms[0]->slug;
  }
  return str_replace('%product_brand%', $client, $post_link);
  return $post_link;
}

add_filter('post_type_link', 'custom_post_link', 1, 3);

0 个答案:

没有答案