bug*_*er9 2 wordpress permalinks wordpress-theming custom-fields custom-taxonomy
我们正在3个国家/地区建立汽车相关业务目录 - 美国,加拿大和墨西哥.
我创建了一个自定义帖子类型"列表",并尝试继续以下内容.
对于各种SEO目的,我们需要特定的URL结构,如下所示:
1)个人列表页面
没什么太花哨的 - domain.com/usa/listing-slug
2)每州档案
没什么太好看的 - domain.com/usa/texas
3)每个城市的档案
这个更有意思,我们需要以下结构:
第一个是俄勒冈州克利夫兰的档案馆,另一个是内华达州克利夫兰的档案馆.
4)每ZIP存档
很平常 - domain.com/usa/05657
5)混合位置类别档案
6)以上所有存档页面的自定义内容(模板),可能是ZIP
我们试图在某种程度上模仿这个网站.
如果你检查一下,比方说,克利夫兰,俄亥俄州克利夫兰和内华达州的同样的例子:http://www.familydaysout.com/kids-things-to-do-usa/cleveland/oh和HTTP://www.familydaysout .com/kids-things-to-do-usa/cleveland/nd你会明白我的意思 - 这些页面在开头就有自定义内容(描述).
以上是每个城市的自定义内容示例.每个州http://www.familydaysout.com/kids-things-to-do-usa/ohio和混合的情况也是如此:http : //www.familydaysout.com/kids-things-to-do-usa/ohio/major-theme-parks http://www.familydaysout.com/kids-things-to-do-usa/cleveland/oh/major-theme-parks http://www.familydaysout.com/kids-things -to-DO-USA/5601 /重大主题,公园
我在添加新列表时可以自动填充位置数据,所以当我粘贴一个完整的地址时,例如"21200 Saint Clair Avenue Euclid,Ohio 44117"并保存我得到这个:
如您所见,国家,州,城市和ZIP都在那里.类别(汽车零件)也作为列表类别.
现在,我如何使用这些值来构建存档的URL和页面/模板?我应该自动将自定义字段值转换为自定义分类法并从那里开始吗?我知道我可以使用https://codex.wordpress.org/Plugin_API/Action_Reference/save_post和https://codex.wordpress.org/Function_Reference/wp_set_object_terms来做到这一点
我知道自定义分类法可以有描述,我知道如何输出这些描述.除此之外,分类法可以是等级的,所以我可以将俄亥俄州和内华达州作为父母,每个克利夫兰作为他们的孩子.
所以我正处于收集和存储所有信息位的位置,但不知道如何开始操作它们来实现上述模型.
问题1:我应该在哪里存储位置数据(国家,州,城市,邮编),以便能够在URL中使用它,如上所述?自定义字段或分类术语?
Q2:如何以我可以为他们自定义模板的方式构建存档页面/模板?
任何其他有用的提示将受到高度赞赏.
搜索后,我想我现在可以帮到你了.
location分类以创建基本存档slug:function wpso36667674_register_location_taxonomy() {
$labels = [
'name' => _x('Locations', 'Taxonomy General Name', 'wpso'),
'singular_name' => _x('Location', 'Taxonomy Singular Name', 'wpso'),
'all_items' => __('All Locations', 'wpso'),
];
$args = [
'labels' => $labels,
'public' => true,
'hierarchical' => true,
'rewrite' => ['hierarchical' => true],
];
register_taxonomy('location', 'listing', $args);
}
add_action('init', 'wpso36667674_register_location_taxonomy', 0);
Run Code Online (Sandbox Code Playgroud)
我们必须将WordPress自动生成的术语"slug"更改为已清理的术语名称,以便URL的结构看起来如您所愿:
function wpso36667674_filter_term_link($term_link, $term, $taxonomy) {
$term_link = rtrim($term_link, '/');
$pos = strrpos($term_link, '/');
$term_link = substr($term_link, 0, $pos + 1) . sanitize_title($term->name);
return $term_link;
}
add_filter('term_link', 'wpso36667674_filter_term_link', 0, 3);
Run Code Online (Sandbox Code Playgroud)
注意:添加新术语时不要手动输入术语slug,让WordPress为我们生成术语,否则我们将无法正确查询术语的帖子内容.原因是两个URL:location/usa/ohio/cleveland并location/usa/idaho/cleveland给我们相同的帖子,因为WordPress仅用cleveland作查询帖子内容的术语而不关心父词.
listing帖子类型:function wpso36667674_register_listing_post_type() {
$labels = [
'name' => _x('Listings', 'Taxonomy General Name', 'wpso'),
'singular_name' => _x('Listing', 'Taxonomy Singular Name', 'wpso'),
'all_items' => __('All Listings', 'wpse'),
];
$args = [
'labels' => $labels,
'public' => true,
'menu_icon' => 'dashicons-location-alt',
'menu_position' => 6,
'supports' => ['title', 'editor', 'thumbnail', 'custom-fields'],
'taxonomies' => ['location'],
'rewrite' => ['slug' => '%country%/%state%/%city%/%zip%'],
];
register_post_type('listing', $args);
}
add_action('init', 'wpso36667674_register_listing_post_type', 0);
Run Code Online (Sandbox Code Playgroud)
我们不确定该列表是否具有州| zip | zip,因此我们必须使用substitutes(%country%/%state%/%city%/%zip%).然后将其替换为每个列表元数据(根据我使用WordPress的经验,您应该在每个列表中存储位置数据):
function wpso36667674_filter_post_link($post_link, $post, $leave_name = false, $sample = false) {
$zip = get_post_meta($post->ID, 'geolocation_postcode', true);
$city = get_post_meta($post->ID, 'geolocation_city_short', true);
$state = get_post_meta($post->ID, 'geolocation_state_short', true);
$country = get_post_meta($post->ID, 'geolocation_country_short', true);
$post_link = str_replace('%zip%', $zip, $post_link);
$post_link = str_replace('%city%', $city, $post_link);
$post_link = str_replace('%state%', $state, $post_link);
$post_link = str_replace('%country%', $country, $post_link);
$post_link = preg_replace('/[^:](\/{2,})/', '/', $post_link); // Remove multiple forward slashes except http://.
return $post_link;
}
add_filter('post_type_link', 'wpso36667674_filter_post_link', 0, 4);
Run Code Online (Sandbox Code Playgroud)
注意:如果使用大写的短名称,则在修改URL的结构时必须将其转换为小写.
// Add rewrite rules for location taxonomy.
function wpso36667674_add_location_rewrite_rules() {
add_rewrite_rule('^location/([^/]+)/?$', 'index.php?taxonomy=location&term=$matches[1]', 'top');
add_rewrite_rule('^location/([^/]+)/([^/]+)/?$', 'index.php?taxonomy=location&term=$matches[2]', 'top');
add_rewrite_rule('^location/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?taxonomy=location&term=$matches[3]', 'top');
}
add_action('init', 'wpso36667674_add_location_rewrite_rules', 0);
// Add rewrite rules for listings.
function wpso36667674_add_listing_rewrite_rules() {
add_rewrite_rule('^usa/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[1]', 'top');
add_rewrite_rule('^usa/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[2]', 'top');
add_rewrite_rule('^usa/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[3]', 'top');
add_rewrite_rule('^usa/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[4]', 'top');
add_rewrite_rule('^mexico/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[1]', 'top');
add_rewrite_rule('^mexico/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[2]', 'top');
add_rewrite_rule('^mexico/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[3]', 'top');
add_rewrite_rule('^mexico/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[4]', 'top');
add_rewrite_rule('^canada/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[1]', 'top');
add_rewrite_rule('^canada/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[2]', 'top');
add_rewrite_rule('^canada/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[3]', 'top');
add_rewrite_rule('^canada/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$', 'index.php?post_type=listing&name=$matches[4]', 'top');
}
add_action('init', 'wpso36667674_add_listing_rewrite_rules', 0);
Run Code Online (Sandbox Code Playgroud)
正如我所说,通过更改usa/idaho/cleveland-idaho为usa/idaho/cleveland永久链接,查询的帖子usa/idaho/cleveland与...相同usa/ohio/cleveland.因此,我们必须帮助WordPress了解用于查询帖子内容的正确术语slug:
function wpso36667674_modify_requested_url($query) {
if ( $query->query_vars['taxonomy'] === 'location' ) {
$slugs = array_filter( explode('/', $query->request) );
$term = array_pop($slugs) . '-' . array_pop($slugs);
if ( get_term_by('slug', $term, 'location') ) {
$query->query_vars['term'] = $term;
}
}
}
add_action('parse_request', 'wpso36667674_modify_requested_url');
Run Code Online (Sandbox Code Playgroud)
感谢@Jevuska建议使用parse_request.
感谢WordPress模板层次结构,我们可以轻松完成.
例:
对于location/usa/ohio,模板将是taxonomy-location-ohio.php.
但请记住,我们必须使用WordPress生成的实际术语,而不是我们在URL中修改的术语.
例:
如果俄亥俄州的克利夫兰在爱达荷州的克利夫兰之前加入,那么第一个是cleveland第二个,第二个是第二个cleveland-idaho.
然后,第一个是taxonomy-location-cleveland.php第二个模板,第二个是taxonomy-location-cleveland-idaho.php.
就这样!所有代码都可以直接放在functions.php文件中.请记住也要刷新永久链接设置.
| 归档时间: |
|
| 查看次数: |
1105 次 |
| 最近记录: |