【问题标题】:Permalink from meta fields for custom post type wordpress自定义帖子类型 wordpress 的元字段的永久链接
【发布时间】:2014-06-09 12:35:17
【问题描述】:

我的 wordpress 实例中有 5 个自定义字段,它们是布尔真/假值。

property_type_investment、property_type_office、property_type_rental、property_type_industrial、property_type_land

是否可以为此自定义帖子类型创建一个永久链接,以检查每个元值是否为真 - 如果是这样,将一个值附加到带连字符的 url?

示例:如果 property_type_investment、property_type_office 和 property_type_land 为真。我希望永久链接是...

/listings/investment-office-land/post-title/

这是我的注册帖子类型。

register_post_type( 'properties',
    array(
        'labels' => array(
            'name' => 'Properties',
            'singular_name' => 'Property',
            'add_new' => 'Add New',
            'add_new_item' => 'Add New Property',
            'edit' => 'Edit',
            'edit_item' => 'Edit Property',
            'new_item' => 'New Property',
            'view' => 'View',
            'view_item' => 'View Property',
            'search_items' => 'Search Properties',
            'not_found' => 'No Property found',
            'not_found_in_trash' => 'No Properties found in Trash',
            'parent' => 'Parent Property'
        ),

        'public' => true,
        'rewrite' => array('slug'=>'listings'),
        'menu_position' => 15,
        'supports' => array( 'title', 'editor', 'comments', 'thumbnail', 'custom-fields' ),
        'taxonomies' => array( '' ),
        'has_archive' => true
    )
);

【问题讨论】:

    标签: wordpress custom-post-type permalinks


    【解决方案1】:

    你可以重写规则。

    function prefix_investment_rewrite_rule() {
        add_rewrite_rule( 'listings/([^/]+)/investment-office-land', 'index.php?listings=$matches[1]&investment-office-land=yes', 'top' );
    }
    
    add_action( 'init', 'prefix_investment_rewrite_rule' );
    
    //You should register the query var
    function prefix_register_query_var( $vars ) {
        $vars[] = 'investment-office-land';
        return $vars;
    }
    
    add_filter( 'query_vars', 'prefix_register_query_var' );
    
    // assign a template
    function prefix_url_rewrite_templates() {
         if ( get_query_var( 'investment-office-land' ) && is_singular( 'listings' ) ) {
            add_filter( 'template_include', function() {
                return get_template_directory() . '/single-listings-investment-office-land.php';
            });
        }
    }
    
    add_action( 'template_redirect', 'prefix_url_rewrite_templates' );
    

    不要忘记在 register_post_type 之后刷新重写规则。

    flush_rewrite_rules();  
    

    【讨论】:

    • 感谢您的回复...看来此解决方案仅适用于投资-办公-土地组合。我需要它能够根据我列出的这 5 个字段解决所有可能的组合。示例:/investment/、/office-rental/、/investment-office-rental-land-industrial/ 等...
    • 你能检查一下这个link。这深刻地解释了我上面写的任何内容。
    【解决方案2】:

    您可以在管理面板->设置->永久链接->自定义结构中使用/%category%/%postname%/通过简单的方式进行测试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      相关资源
      最近更新 更多