首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress CPT URL重写

Wordpress CPT URL重写
EN

Stack Overflow用户
提问于 2015-03-11 20:58:28
回答 1查看 296关注 0票数 2

我正在尝试重建我的WordPress站点的url,但是没有成功。

我有3个自定义帖子类型,行,集合和产品。其思想是通过一个层次结构(一对多)将它们联系在一起:产品线、集合、产品

因此,如果我有一个名为L的行,一个名为C的集合和一个产品P,那么我想像这样访问:

A行应该类似于site.com/l

A列应类似site.com/l/c

产品A应类似site.com/l/c/p

我已经写了这段代码,但是不能正常工作。

按照重写规则的顺序,所有3个urls都返回A行。

但是,如果我颠倒顺序,编写重写规则,将产品放在第一位,集合放在第二位,行放在第三位,行为就会改变。

使用倒置时,url site.com/l返回A行,而url site.com/l/c和site.com/l/c/p返回A列。

我不知道为什么我不能访问产品页面。

顺便说一下,我使用类型插件来生成CPT,并使用ACF插件来生成带有CPT之间关系的元字段(这就是为什么我有函数"get_field")

代码语言:javascript
复制
// BACK_END
add_filter('post_type_link', 'site_type_permalink', 10, 4);

function site_type_permalink($post_link, $post, $leavename, $sample) {

$permalink = $post_link;

if ($post->post_type == 'lines') {
    $permalink = str_replace('lines/', '', $post_link);
}

if ($post->post_type == 'collections') {
    global $post;

    $lines = get_field('line_obj');
    $title = $lines[0]->post_name;

    $permalink = str_replace('collections/', $title . '/', $post_link);
}

if ($post->post_type == 'products') {
    global $post;

    $col = get_field('collection_obj');
    $colSlug = $col[0]->post_name;
    $lin = get_field('line_obj', $col->ID);
    $linSlug = $lin[0]->post_name;

    $permalink = str_replace('products/', $linSlug . '/' . $colSlug . '/', $post_link);
 }

 return $permalink;
}


// FRONT_END
function site_rewrite_rules() {

// lines
add_rewrite_rule(
  '((?!blog|wp-json)[^/]*)/?',
  'index.php?lines=$matches[1]',
  'top'
);


// collections
add_rewrite_rule(
  '((?!blog|wp-json)[^/]*)/([^/]*)/?',
  'index.php?lines=$matches[1]&collections=$matches[2]',
  'top'
);


// products
add_rewrite_rule(
  '((?!blog|wp-json)[^/]*)/([^/]*)/([^/]*)/?',
  'index.php?lines=$matches[1]&collections=$matches[2]&products=$matches[3]',
  'top'
);

}
add_action('init', 'site_rewrite_rules', 10, 0);

有人知道我做错了什么吗?谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-03-12 02:32:28

产品线,系列,产品...它们都只是产品。创建一个分类有序的帖子类型,并为查询适当产品的行/集合创建页面。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28987228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档