Wordpress 奇特的URL结构、条件内容和搜索—使用CPT、自定义字段和分类法
我们正在建立美国、加拿大和墨西哥三个国家的汽车相关业务目录 我创建了一个自定义帖子类型“Listings”,并尝试继续以下内容 出于各种SEO目的,我们需要特定的URL结构,如下所示: 1)个人登录页面 没什么特别的-domain.com/usa/listing slug 2)每个州的档案 也没什么特别的-domain.com/usa/texas 3)每个城市的档案 这个更有趣,我们需要以下结构:Wordpress 奇特的URL结构、条件内容和搜索—使用CPT、自定义字段和分类法,wordpress,wordpress-theming,permalinks,custom-fields,custom-taxonomy,Wordpress,Wordpress Theming,Permalinks,Custom Fields,Custom Taxonomy,我们正在建立美国、加拿大和墨西哥三个国家的汽车相关业务目录 我创建了一个自定义帖子类型“Listings”,并尝试继续以下内容 出于各种SEO目的,我们需要特定的URL结构,如下所示: 1)个人登录页面 没什么特别的-domain.com/usa/listing slug 2)每个州的档案 也没什么特别的-domain.com/usa/texas 3)每个城市的档案 这个更有趣,我们需要以下结构: domain.com/usa/cleveland/oh domain.com/usa/cleve
- domain.com/usa/cleveland/oh
- domain.com/usa/cleveland/nd
- domain.com/usa/cleveland/oh/car parts
- domain.com/usa/ohoio/car parts
- domain.com/usa/05657/car parts
我们将非常感谢任何其他有用的提示。根据您的要求,您可以通过创建以下类别来实现这一点:
domain.com/usa/cleveland/oh
和domain.com/usa/cleveland/nd
USA->Cleveland->oho
,在slug中应该是USA->Cleveland->oh
类似地,USA->Cleveland->Nevada
和slug作为USA->Cleveland->nd
现在,对于要在URL上显示的帖子:
类别
。为此,请在根文件夹中的.htaccess
文件中编写以下代码
RewriteRule . /wordpress/index.php [L]
我认为有两种方法可以解决你的问题 第一种方法是使用mod_rewrite将您建议的格式化URL传输到关联模板php文件,并以您想要的方式处理您的请求。 下面的链接可能会有所帮助。谷歌搜索更多信息 第二种方法是遵循wordpress页面模板层次结构,相应地创建模板页面,并在模板php中处理类似的请求。有关详细说明,请参阅
还有一个快速页面模板层次结构,以了解针对特定url将执行哪个页面模板。请参考在搜索之后,我想我现在可以帮助您了 1.注册
位置
分类法以创建基本存档slug:
我们必须将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);
注意事项:添加新术语时,不要手动输入术语的slug,让WordPress为我们生成,否则我们将无法正确查询术语的帖子内容。原因是两个URL:location/usa/ohoio/cleveland
和location/usa/idaho/cleveland
为我们提供了相同的帖子,因为WordPress只使用cleveland
作为查询帖子内容的术语,而不考虑父术语
2.注册listing
post类型如下:
我们不确定列表是否有state | city | zip,因此我们必须使用替代品(%country%/%state%/%city%/%zip%
)。然后用每个列表元数据替换它(根据我使用WordPress的经验,您应该在每个列表中存储位置数据):
注意:如果您使用大写短名称,则在修改URL结构时必须将其转换为小写
3.添加重写规则以使自定义URL正常工作。
4.返回正确的术语段塞:
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);
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);
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);
// 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);
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');