【问题标题】:Wordpress CPT URL rewriteWordpress CPT URL 重写
【发布时间】:2015-03-11 12:58:28
【问题描述】:

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

我有 3 种自定义帖子类型、行、集合和产品。 想法是它们通过层次结构(一对多)链接:行、集合、产品

所以,如果我有一个名为 L 的行、一个名为 C 的集合和一个产品 P,我想像这样访问:

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

Col A 应该类似于 site.com/l/c

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

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

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

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

通过反转,url site.com/l 返回 Line A,urls site.com/l/c 和 site.com/l/c/p 返回 Col A。

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

顺便说一句,我使用 Types 插件来生成 CPT 和 ACF 插件来生成具有 CPT 之间关系的元字段(这就是为什么我有函数“get_field”)

// 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);

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

【问题讨论】:

  • “它不起作用”并没有告诉我们问题出在哪里。它“不起作用”怎么办?错误?白屏?还有什么?
  • 你是绝对正确的。我已将问题编辑得更清楚。

标签: php wordpress rewrite permalinks


【解决方案1】:

系列、系列、产品……它们都只是产品。使用有组织的分类法创建一种帖子类型,并为查询相应产品的行/集合创建页面。

【讨论】:

  • 当然这是一个选项。也许是最合乎逻辑的一个。但是将线条和集合作为分类法;我如何保证层次线->集合?
  • 您可以使用层次结构创建一种分类法 - 父项和子项。然后你可以有 site.com/line/collection/product codex.wordpress.org/Taxonomies
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-25
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-26
相关资源
最近更新 更多