精美的URL结构,条件内容和搜索-使用CPT,自定义字段和分类

我们正在建立3个国家(美国, 加拿大和墨西哥)与汽车相关的业务的目录。
我创建了一个自定义帖子类型” 列表” , 并尝试进行以下操作。
出于各种SEO目的, 我们需要如下特定的URL结构:
1)个人列表页面
没什么花哨的-domain.com/usa/listing-slug
2)每个州的档案
也没什么花哨的-domain.com/usa/texas
3)每个城市的档案
这个比较有趣, 我们需要以下结构:

  • domain.com/usa/cleveland/oh
  • domain.com/usa/cleveland/nd
第一个是俄亥俄州克利夫兰的档案馆, 另一个是内华达州克利夫兰的档案馆。
4)每个ZIP档案
很平常-domain.com/usa/05657
5)混合位置类别档案
  • domain.com/usa/cleveland/oh/car-parts
  • domain.com/usa/ohio/car-parts
  • domain.com/usa/05657/car-parts
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, 俄亥俄州44117″ , 然后保存, 我得到了:
精美的URL结构,条件内容和搜索-使用CPT,自定义字段和分类

文章图片
如你所见, 国家, 州, 城市和邮政编码都在那儿。类别(汽车零件)也作为列表类别存在。
现在, 如何使用这些值来构造档案的URL和页面/模板?我是否应该将自定义字段值自动转换为自定义分类法, 然后从那里去?我知道我可以使用https://codex.wordpress.org/Plugin_API/Action_Reference/save_post和https://codex.wordpress.org/Function_Reference/wp_set_object_terms完成此操作
我知道自定义分类法可以有描述, 并且我知道如何输出这些描述。除此之外, 分类法可以是分层的, 因此我可以将俄亥俄州和内华达州作为父母, 每个克利夫兰都作为孩子。
因此, 我正设法收集和存储所有信息位, 但不确定如何开始操纵它们以实现上述模型。
问题1:我应该在哪里存储位置数据(国家, 州, 城市, 邮政编码), 以便能够在上述URL中使用它?自定义字段或分类术语?
问题2:如何以可以自定义模板/模板的方式如上所述构建归档页面/模板?
任何其他有用的提示将不胜感激。
#1 经过四处搜寻, 我想现在可以为你提供帮助。
1.注册位置分类法以创建基本归档段:
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);

我们必须将由WordPress自动生成的术语词条更改为经过消毒的术语名称, 以使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);

注意:添加新术语时, 请勿手动输入术语的子句, 让WordPress为我们生成它, 否则我们将无法正确查询术语的帖子内容。原因是两个URL:location / usa / ohio / cleveland和location / usa / idaho / cleveland给我们相同的帖子, 因为WordPress仅使用cleveland作为查询帖子内容的术语, 而无需关心父术语。
2.注册列表帖子类型, 如下所示:
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);

我们不确定清单中是否包含state | city | zip, 因此我们必须使用替代项(%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);

注意:如果使用大写的短名称, 则在修改URL的结构时必须将其转换为小写。
3.添加重写规则以使我们的自定义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);

4.返回正确条款的内容:
如我所说, 通过永久链接将usa / idaho / cleveland-idaho更改为usa / idaho / cleveland, 查询到的usa / idaho / cleveland帖子与usa / ohio / cleveland相同。因此, 我们必须帮助WordPress知道用于查询帖子内容的正确词条是什么:
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');

感谢@Jevuska建议使用parse_request。
5.为每个存档位置创建一个自定义模板:
多亏了WordPress模板层次结构, 我们可以轻松做到这一点。
例子:
对于location / usa / ohio, 模板将为taxonomy-location-ohio.php。
但是请记住, 我们必须使用由WordPress生成的实际术语的词条, 而不是我们在URL中修改的词条。
例子:
如果在爱达荷州的克利夫兰之前增加俄亥俄州的克利夫兰, 则第一个的子弹头是克利夫兰, 第二个的子弹头是克利夫兰-爱达荷州。
然后, 第一个模板是taxonomy-location-cleveland.php, 第二个模板是taxonomy-location-cleveland-idaho.php。
就这样!你可以将所有代码直接放在functions.php文件中。记住也要刷新永久链接设置。
#2 根据你的要求, 可以通过创建如下所述的类别来实现此目的:
domain.com/usa/cleveland/oh和domain.com/usa/cleveland/nd
  1. 俄亥俄州克利夫兰(/美国/克利夫兰/俄亥俄州)
  2. 内华达州克利夫兰(/美国/克利夫兰/ nd)
【精美的URL结构,条件内容和搜索-使用CPT,自定义字段和分类】因此, 将父类别创建为” USA” , 然后将子类别创建为” Cleveland” , 然后在该子类别下创建另一个子类别为” Ohio” , 并将其子项命名为” oh” 。
因此, 层次结构应该是USA-> Cleveland-> Ohio, 而应该是usa-> Cleveland-> oh
同样, USA-> Cleveland-> 内华达州和为usa-> Cleveland-> nd
现在, 要在URL中显示帖子:
  1. domain.com/category/usa =为他们提供” 美国” 类别
  2. domain.com/category/usa/cleveland/ =为他们提供” 克利夫兰” 类别
  3. domain.com/category/usa/cleveland/oh =为他们提供” 俄亥俄州” 类别
现在, 你只需要从URL删除类别。因此, 将以下代码写入位于根文件夹的.htaccess文件中。
RewriteRule . /wordpress/index.php [L]

#3 我认为有两种方法可以解决你的问题, 第一种是使用mod_rewrite来传输你建议的格式化URL, 以关联模板php文件并在那里以所需方式处理你的请求。为此, 以下链接可能会有所帮助。谷歌获取更多https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
第二种方法是遵循wordpress页面模板层次结构, 并相应地创建模板页面, 并在模板php中处理类似的请求。有关详细说明, 请参阅https://www.srcmini.com/2015/06/wordpress-custom-page-templates/
有关了解特定网址将执行哪个页面模板的快速页面模板层次结构, 请参阅https://media-mediatemple.netdna-ssl.com/wp-content/uploads/2015/05/01-wp-template-hierarchy- opt.jpg

    推荐阅读