问题描述
我的项目是公司列表的目录.
My project is a directory of business listings.
单个列表URL看起来像 domain.com/five-star-cars 一样简单-WordPress可以立即使用.
Individual listing URL looks as simple as domain.com/five-star-cars - something that WordPress handles out of the box.
但是我在钻研基于地区的档案时遇到了问题.我们需要针对这些URL的特定URL结构.例如,如果要显示俄亥俄州的所有列表,则需要 domain.com/ohio -也很简单.
But I have a problem drilling into region-based archives. We need a specific URL structure for those. For example, if we want to show all listings in Ohio, we need domain.com/ohio - also simple.
但是,如果我们想显示俄亥俄州克利夫兰和内华达州克利夫兰的所有列表-我们想这样做:
However, if we want to show all listings in Cleveland, Ohio and Cleveland, Nevada - we want to do this:
- domain.com/cleveland/oh
- domain.com/cleveland/nd
- domain.com/cleveland/oh
- domain.com/cleveland/nd
因此,我创建了一个CPT列表"和一个自定义分类法"Location".我将俄亥俄州和内华达州作为父母条款,将克利夫兰作为孩子.
So I created a CPT "Listings" and a custom taxonomy "Location". I added Ohio and Nevada as parent terms and Cleveland as children to both.
这是问题所在:第一个克利夫兰的子弹是 domain.com/location/ohio/cleveland/,但是第二个克利夫兰成了 domain.com/location/nevada/cleveland-nevada /
Here's the issue: the slug of the 1st Cleveland is domain.com/location/ohio/cleveland/ but the 2nd one became domain.com/location/nevada/cleveland-nevada/
我发现术语词条必须是唯一的,所以这是一个问题-我不能使用术语词条来构建URL.
I found out that term slugs must be unique, so that's a problem - I can't use term slugs to build URLs.
我接下来要做的是在每个清单中将州和城市保存为自定义字段值.现在,我可以通过以下方式将这些值(与术语slug不同,不必唯一)用作全局变量:
What I did next is saving state and city as custom field values in each listing. Now I can use these values (which, unlike term slugs, don't have to be unique) as global variables via something like this:
function loc_global_vars() {
global $wp_query;
$postid = $wp_query->post->ID;
global $loc_vars;
$loc_vars = array(
'state' => get_post_meta($postid, 'state', true),
'city' => get_post_meta($postid, 'city', true),
);
}
add_action( 'parse_query', 'loc_global_vars' );
但是现在我陷入困境:下一步是什么?
But now I'm stuck: what are my next steps?
第一季度:如何使用这些变量构建存档页面和永久链接?
Q1: How do I build archive pages and permalinks using these variables?
第二季度:如何构建模板,以便能够针对每种存档类型自定义它们?
Q2: How do I build templates so that I'm able to customize them for every archive type?
或者我错过了什么明显的东西,而我只是在寻找一个错误的方向?因此,通常:
Or is there anything obvious that I've missed and I'm simply looking into a wrong direction?So, generally:
第三季度:有没有更好的方法来解决这个问题?
Q3: Is there a better way to approach this?
推荐答案
使用重写API ,则可以使用您的帖子元来构建URL.
Using the Rewrite API, you can use your post meta to build URLs.
1)通过add_rewrite_tag()
2)设置add_rewrite_rule('([^/]+)/?','index.php?state=$matches[1]&city=$matches[2]','top');
3)设置钩子后,例如通过保存永久链接设置@ /wp-admin/options-permalink.php
来刷新重写规则.
3) Flush the rewrite rules after you set your hooks, for example by saving a permalinks setting @ /wp-admin/options-permalink.php
.
此处.
寻址Q3,您可以使用WP路由系统 Cortex .
Addressing Q3, you could use Cortex, a WP routing system.
或者,一种不太优雅的解决方案,为什么不创建两种自定义帖子类型,一种用于城市,一种用于区域,以及一种将城市放在区域页面上的自定义税种?
Or, less inelegant solution, why not create two custom post types, one for cities and one for regions, and a custom tax to put the cities on the region pages?
这篇关于非唯一术语term,或者我可以用来构建特定页面和永久链接的任何其他内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!