Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 奇特的URL结构、条件内容和搜索—使用CPT、自定义字段和分类法_Wordpress_Wordpress Theming_Permalinks_Custom Fields_Custom Taxonomy - Fatal编程技术网

Wordpress 奇特的URL结构、条件内容和搜索—使用CPT、自定义字段和分类法

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

我们正在建立美国、加拿大和墨西哥三个国家的汽车相关业务目录

我创建了一个自定义帖子类型“Listings”,并尝试继续以下内容

出于各种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)每邮政编码存档

非常普通-domain.com/usa/05657

5)混合位置类别存档

  • domain.com/usa/cleveland/oh/car parts
  • domain.com/usa/ohoio/car parts
  • domain.com/usa/05657/car parts
6)上述所有存档页面的自定义内容(模板),可能除了ZIP之外

我们在努力模仿

如果你查看俄亥俄州克利夫兰和内华达州克利夫兰的相同例子,你就会明白我的意思——这些页面在开头有自定义内容(描述)

以上是每个城市的自定义内容示例。每个州和混合州的情况相同:

在添加新列表时,我可以自动填充位置数据,因此当我粘贴完整地址,例如“俄亥俄州圣克莱尔大道21200号欧几里得44117”并保存时,我得到以下信息:

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

现在,我如何使用这些值为归档构建URL和页面/模板?我是否应该自动将自定义字段值转换为自定义分类法并从中开始?我知道我可以用和来做这件事

我知道自定义分类法可以有描述,我知道如何输出这些描述。除此之外,分类法可以是分层的,所以我可以把俄亥俄州和内华达州作为父母,把每个克利夫兰州作为他们的孩子

因此,我正处于收集和存储所有信息位的阶段,但不确定如何开始操纵它们以实现上述模型

Q1:我应该在哪里存储位置数据(国家、州、城市、邮政编码),以便能够在上述URL中使用它?自定义字段还是分类术语

Q2:如何按照上述方式构建归档页面/模板,以便为其自定义模板


我们将非常感谢任何其他有用的提示。

根据您的要求,您可以通过创建以下类别来实现这一点:

domain.com/usa/cleveland/oh
domain.com/usa/cleveland/nd

  • 俄亥俄州克利夫兰(美国/克利夫兰/俄亥俄州)
  • 内华达州克利夫兰(美国/克利夫兰/nd)
  • 因此,创建一个父类为“USA”,然后子类为“Cleveland”,再创建一个子类为“Ohio”,子类为“oh”

    所以层次结构应该是
    USA->Cleveland->oho
    ,在slug中应该是
    USA->Cleveland->oh

    类似地,
    USA->Cleveland->Nevada
    和slug作为
    USA->Cleveland->nd

    现在,对于要在URL上显示的帖子:

  • domain.com/category/usa=为他们提供“美国”类别
  • domain.com/category/usa/cleveland/=为他们提供“克利夫兰” 类别
  • domain.com/category/usa/cleveland/oh=为他们提供“俄亥俄州” 类别
  • 现在您只需要从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');