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

标签 wordpress wordpress-theming permalinks custom-fields custom-taxonomy

我们正在建立 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 除外

我们正在尝试模仿 this website .

如果您检查俄亥俄州克利夫兰和内华达州克利夫兰的相同示例:http://www.familydaysout.com/kids-things-to-do-usa/cleveland/ohhttp://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/major-theme-parks

我已经达到了在添加新列表时可以自动填充位置数据的程度,因此当我粘贴完整地址时,例如“21200 Saint Clair Avenue Euclid, Ohio 44117”并保存我得到这个:

automatically populated custom fields

如您所见,国家、州、城市和邮政编码都在那里。类别(汽车零件)也作为列表类别存在。

现在我如何使用这些值来构建存档的 URL 和页面/模板?我应该自动将自定义字段值转换为自定义分类法并从那里开始吗?我知道我可以用https://codex.wordpress.org/Plugin_API/Action_Reference/save_post来做到这一点和 https://codex.wordpress.org/Function_Reference/wp_set_object_terms

我知道自定义分类法可以有描述,并且我知道如何输出这些描述。除此之外,分类法可以是分层的,因此我可以将俄亥俄州和内华达州添加为父级,并将每个克利夫兰添加为它们的子级。

所以我现在设法收集和存储所有信息位,但不确定如何开始操作它们以实现上述模型。

问题1:我应该在哪里存储位置数据(国家、州、城市、邮政编码),以便能够在如上所述的网址中使用它?自定义字段或分类术语?

问题2:如何构建如上所述的存档页面/模板,以便我可以为其自定义模板?

任何其他有用的提示将不胜感激。

最佳答案

经过一番搜索,我想我现在可以帮助你了。

1。注册 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);

我们必须更改由 WordPress 自动生成的术语“slugs”,以净化术语名称,以便 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/clevelandlocation/usa/idaho/cleveland 为我们提供了相同的帖子,因为 WordPress 仅使用 cleveland 作为查询帖子内容的术语,而不关心父术语。

2。注册 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);

我们不确定列表中是否包含 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);

注意:如果您使用大写短名称,则在修改网址结构时必须将其转换为小写。

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 的帖子美国/俄亥俄/克利夫兰相同。因此,我们必须帮助 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');

感谢@Jevuska建议使用 parse_request

5。为每个存档位置创建自定义模板:

感谢WordPress Template Hierarchy ,我们可以轻松做到。

示例:

对于location/usa/ohio,模板将为taxonomy-location-ohio.php

但请记住,我们必须使用 WordPress 生成的实际术语“slugs”,而不是我们在 URL 中修改的内容。

示例:

如果俄亥俄州的克利夫兰在爱达荷州的克利夫兰之前添加,则第一个的术语 slug 为 cleveland,第二个将为 cleveland-idaho

然后,第一个模板是 taxonomy-location-cleveland.php,第二个模板是 taxonomy-location-cleveland-idaho.php

仅此而已!您可以将所有代码直接放入 functions.php 文件中。请记住也要刷新永久链接设置。

关于wordpress - 精美的 URL 结构、条件内容和搜索 - 使用 CPT、自定义字段和分类法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36667674/

相关文章:

ios - MP4 视频在桌面上工作,而不是在移动设备上

css - 如何修复 Wordpress 中的 float 菜单?

php - Wordpress 在一页上显示所有页面

wordpress - 如何删除自定义帖子类型 url 中的 slug?

jquery - WordPress 固定链接

javascript - 未捕获的类型错误 : $ is not a function bold automatic the tabele from javascript

wordpress - 我如何预览我的 youtube channel 播放列表 wordpress

php - 在 OS X 上安装 wordpress 建立连接时出错

css - 我为我的 wordpress 博客制作了一个固定的导航栏,但它隐藏在我的 wordpress 信息栏后面

WordPress 作者永久链接