问题描述
我正在使用一个名为自定义帖子类型永久链接的插件,这可以帮助我获取一个帖子类型,以与我创建的名为 product_category
,但是在它们前面也可以使用子类别的 / shop /
上,我无法获得正确的product_category分类链接。
I am using this plugin called, "Custom Post Type Permalinks", which kinda helps me to get a post type to line up with a custom taxonomy that I created called, product_category
, but I can't get the product_category taxonomy links proper with /shop/
in front of them that work with Child Categories also.
基本上,自定义帖子类型为 product
,其网址结构如下:/ shop / category / {Category Parent} / {Category Child} / {产品名称},并在注册自定义帖子类型时在functions.php中如下所示:
Basically, the custom post type is product
and has a url structure like so: /shop/category/{Category Parent}/{Category Child}/{Product Name} and looks like the following in functions.php when registering the custom post type:
register_post_type('product', array(
'description' => __('Products'),
'label' => __('products'),
'labels' => array(
'name' => _x('Product', 'Post Type General Name'),
'singular_name' => _x('Product', 'Post Type Singular Name'),
'menu_name' => __('Products'),
'parent_item_colon' => __('Parent Product'),
'all_items' => __('All Products'),
'view_item' => __('View Product'),
'add_new_item' => __('Add New Product'),
'add_new' => __('Add New'),
'edit_item' => __('Edit Product'),
'update_item' => __('Update Product'),
'search_items' => __('Search Product'),
'not_found' => __('Not Found'),
'not_found_in_trash' => __('Not Found in Trash')
),
'taxonomies' => array('product_category'),
'supports' => array('title', 'editor', 'thumbnail'),
'rewrite' => array('slug' => 'shop', 'with_front' => false),
"cptp_permalink_structure" => "/category/%product_category%/%postname%/",
'hierarchical' => false,
'public' => true, // Needs to be set to true to be able to show the endpoints!
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => false,
'show_in_admin_bar' => true,
'query_var' => true,
'menu_position' => 10,
'menu_icon' => 'dashicons-cart',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'page'
)
);
cptp_permalink_structure
键允许我分配%product_category%
,以便在URL中收集类别(父项和子项),并且产品名称是路径的最后一部分。我需要给它一个标签,所以我选择 shop
,以便它具有某些属性,因为标签和shop都应该放在类别之前。所有产品网址均正常运行。看起来像这样: / shop / category / {Category Parent} / {Category Child} / {Product Name} /
The cptp_permalink_structure
key allows me to assign %product_category%
so that the categories (parent and children) are collected in the url and the product name is the last part of the path. I needed to give this a slug, so I choose shop
so that it has something as the slug and shop should come before category anyways. All of the Product urls are working perfectly fine. And look like this: /shop/category/{Category Parent}/{Category Child}/{Product Name}/
现在是自定义分类法,我在由WordPress的 init
动作触发的函数中添加了以下内容:
Now for the custom taxonomy, I added within the function that gets triggered by the init
action of WordPress and looks like this:
// I do not want to use 'category' since I don't want post categories mixed with product categories.
register_taxonomy('product_category', 'product', array(
'label' => __('Category'),
'rewrite' => array('slug' => 'shop/category', 'hierarchical' => true, 'with_front' => false),
'labels' => array(
'name' => __('Categories'),
'singular_name' => __('Category'),
'all_items' => __('All Categories'),
'edit_item' => __('Edit Category'),
'view_item' => __('View Category'),
'update_item' => __('Update Category'),
'add_new_item' => __('Add New Category'),
'new_item_name' => __('New Category Name'),
'parent_item' => __('Parent Category'),
'parent_item_colon' => __('Parent Category:'),
'search_items' => __('Search Categories'),
'popular_items' => __('Popular Categories'),
'separate_items_with_commas' => __('Separate Categories with commas'),
'add_or_remove_items' => __('Add or Remove Categories'),
'choose_from_most_used' => __('Choose from the most used Categories'),
'not_found' => __('No Categories found')
),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'hierarchical' => true,
'exclude_from_search' => false,
'description' => 'Categories associated with Products.',
'query_var' => true,
'capabilities' => array(
'manage_terms' => 'manage_options', //by default only admin
'edit_terms' => 'manage_options',
'delete_terms' => 'manage_options',
'assign_terms' => 'edit_posts' // means administrator', 'editor', 'author', 'contributor'
)
));
我尝试使用 shop / category
对于分类法的重写段(如您在上面看到的),如果仅子类别存档页面未生成404页,则将是完美的。因此,虽然: / shop / category / {Category Parent} /
和 / shop / category / {Category Child} /
正常工作, / shop / category / {Category Parent} / {Category Child} /
返回404错误页面。因此,我对于如何使子类别显示为 / shop / category / {Category Parent} / {Category Child} /
I've tried using shop/category
for the rewrite slug of the taxonomy (as you can see above), which would be perfect, if only child category archive pages were not producing 404 pages. So while: /shop/category/{Category Parent}/
and /shop/category/{Category Child}/
works, /shop/category/{Category Parent}/{Category Child}/
returns a 404 Error page. So, I'm at a loss on how to get Child Categories showing as in /shop/category/{Category Parent}/{Category Child}/
注意:如果我改成类别
作为子词,则儿童类别将正确排列,如下所示: / category / {Category Parent} / {Category Child}
,但问题是,在 / shop / > / category / 此处。
Note: If I change to just category
as the rewrite slug, Children Categories are lining up properly like so: /category/{Category Parent}/{Category Child}
, but the problem is, I need /shop/
before /category/
here.
如何执行此操作?几天来一直试图通过多种方式弄清楚这一点,还尝试添加一个重写规则(将重写标记更改为类别
时)。
How to do this? Been trying to figure this out for days now, with tons of different ways, tried adding a rewrite rule also (when changing the rewrite slug to just category
).
add_rewrite_rule('shop/category/?([^/]*)', 'index.php?product_category=$matches[1]', 'top');
但是重写规则对子类别也无效。并返回父类别存档而不是子类别。
But the rewrite rule doesn't work for Child Categories either. And returns the parent category archive instead of the child.
正如我在archive.php文件中使用以下代码测试过的那样:
As I've tested this within the archive.php file with the following code:
$tax_slug = get_query_var('product_category');
echo '<pre>', var_dump($tax_slug), '</pre>';
并返回父类别的子弹,而不是子类别的子弹。
And returns the parent category slug instead of the child on child categories.
在这里感谢您的帮助,我一直在拔头发...秃顶!
Any help is appreciated here, I've been pulling my hair out... going bald!
编辑-可能的方法可能有效
因此,从根本上讲,这可能很简单,就像为此创建另一个 BASE
管理员固定链接部分中的分类法。但是,如何为自定义分类法创建BASE?已经有一个用于类别和标签的BASE,但是如果可行,我将需要 product_category
的BASE,以及 product_category
分类法可能只是类别
,并且BASE会在所有链接之前添加 shop
。但是,如何为自定义分类法创建BASE?
So, basically, it might be as simple as creating another BASE
for that taxonomy in the Permalinks section of the admin. But how to create a BASE for a custom taxonomy? There is already a BASE for categories and tags, but if this could work, I would need a BASE for product_category
, and the slug for the product_category
taxonomy could just be category
, and the BASE would add shop
before it for all links. But how to create a BASE for a custom taxonomy?
推荐答案
在查看 rewrite_rules_array
过滤器。
我在wordpress init
操作,现在可以使用:
I added in this bit in the wordpress init
action and now works:
add_rewrite_rule('shop/category/(.+?)/?$', 'index.php?product_category=$matches[1]&cpage=$matches[2]', 'top');
感谢大家的帮助。自定义分类法的子段在以下位置是相同的:商店/类别
Thanks to all for your help with this. The slug for the custom taxonomy is the same at: shop/category
这里重要的是子类别的工作方式如下:& cpage = $ matches [2]
我不知道,甚至都不知道子类别的名称是 cpage
,这是在查看我刚刚转储到页面上以查看的rewrite_rules_array输出时发现的。
The important thing here in order to get child categories working is this bit: &cpage=$matches[2]
which I wan't aware of and also didn't even know the name of the child category was cpage
, this was discovered while looking at the rewrite_rules_array output which I just dumped on the page to see.
这篇关于WordPress子类别在网址中无法正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!