如何在另一个自定义帖子类型中使用自定义帖子类型slug slug

发布于 2025-01-22 02:33:12 字数 2218 浏览 0 评论 0原文

我在WordPress项目中创建了两个自定义帖子类型: City 属性使用以下查询。

register_post_type('city',
            array(
                'labels' => array(
                    'name' => __('City', 'dc_qode'),
                    'singular_name' => __('City', 'dc_qode'),
                ),
                'public'    =>  true,
                'show_in_menu'  =>  true,
                'rewrite'     =>  array('slug' => 'city'),
                'show_ui'   =>  true,
                'has_archive' =>  false,
                'hierarchical'  =>  true,
                'show_tagcloud' => false,
                'supports' => array(
                    'title',
                    'editor',
                    'thumbnail',
                ),
                'can_export' => true,
                'taxonomies' => array( 'city'),
            )
        );


register_post_type('property',
            array(
                'labels' => array(
                    'name' => __('Property', 'dc_qode'),
                    'singular_name' => __('Property', 'dc_qode'),
                ),
                'public'    =>  true,
                'show_in_menu'  =>  true,
                'rewrite'     =>  array('slug' => 'property'),
                'show_ui'   =>  true,
                'has_archive' =>  false,
                'hierarchical'  =>  true,
                'show_tagcloud' => false,
                'supports' => array(
                    'title',
                    'editor',
                    'thumbnail',
                ),
                'can_export' => true,
                'taxonomies' => array( 'property'),
            )
        );

使用此信息,我可以使用URL http://domain-name.com/property/property-name访问任何属性。但是我想以http://domain-name.com/city-name/property-name(例如http://domain-name.com/toronto)访问URL。 /abcproperty)。城市名称将与每个属性一起分配。我试图在财产中使用slug“城市”,以

'rewrite' => array('slug' => '%city%') 

代替

'rewrite'     =>  array('slug' => 'property')

:但行不通。

我该如何达到这种情况?

I have created two custom post types in my wordpress project: city and property using the below query.

register_post_type('city',
            array(
                'labels' => array(
                    'name' => __('City', 'dc_qode'),
                    'singular_name' => __('City', 'dc_qode'),
                ),
                'public'    =>  true,
                'show_in_menu'  =>  true,
                'rewrite'     =>  array('slug' => 'city'),
                'show_ui'   =>  true,
                'has_archive' =>  false,
                'hierarchical'  =>  true,
                'show_tagcloud' => false,
                'supports' => array(
                    'title',
                    'editor',
                    'thumbnail',
                ),
                'can_export' => true,
                'taxonomies' => array( 'city'),
            )
        );


register_post_type('property',
            array(
                'labels' => array(
                    'name' => __('Property', 'dc_qode'),
                    'singular_name' => __('Property', 'dc_qode'),
                ),
                'public'    =>  true,
                'show_in_menu'  =>  true,
                'rewrite'     =>  array('slug' => 'property'),
                'show_ui'   =>  true,
                'has_archive' =>  false,
                'hierarchical'  =>  true,
                'show_tagcloud' => false,
                'supports' => array(
                    'title',
                    'editor',
                    'thumbnail',
                ),
                'can_export' => true,
                'taxonomies' => array( 'property'),
            )
        );

Using this, I can access any property using the url http://domain-name.com/property/property-name. But I want to access the url as http://domain-name.com/city-name/property-name (for eg. http://domain-name.com/toronto/abcproperty). The city-name will be assigned with each property. I tried to use the slug, 'city', within property as:

'rewrite' => array('slug' => '%city%') 

in place of

'rewrite'     =>  array('slug' => 'property')

But it's not working.

How can I achieve this scenario?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

永言不败 2025-01-29 02:33:12

您可以发表自定义帖子以拥有像页面这样的父母。因此,您必须要做三件事:

  1. 让您自定义帖子支持页面属性。

  2. 您必须注册新的重写规则,然后将定义新的permasustruct,该规则将在第三步中使用。

  3. 注册了新的重写规则后,您应将此规则应用于自定义帖子的永久链接。

    完成工作代码

      add_action('init',function(){
        $ labels = array(
            “名称” => “城市”,
            “ singular_name” => “城市”,
            “菜单_name” => “城市”,
            “ all_items” => “所有城市”,
            “ add_new” => “添加新”,
            “ add_new_item” => “加入新城市”,
            “编辑” => “编辑”,
            “ edit_item” => “编辑城市”,
            “ new_item” => “新城市”,
            “查看” => “看法”,
            “ view_item” => “查看城市”,
            “ search_items” => “搜索城市”,
            “ not_found” => “找不到城市”,
            “ not_found_in_trash” => “在垃圾中没有发现的城市”,
            “父” => “母城”,
        );
    
        $ args = array(
            “标签” => $标签,
             “描述” => “”,
            “ public” =>真的,
            “ show_ui” =>真的,
            “ has_archive” =>真的,
            “ show_in_menu” =>真的,
            “ dubl_from_search” =>错误的,
            “ caperability_type” => “邮政”,
            “ map_meta_cap” =>真的,
            “层次结构” =>真的,
            “重写” => array(“ slug” =>“ city”,“ with_front” => true),
            “ query_var” =>真的,
            “支持” =>阵列(“标题”,“修订”,“缩略图”)
        );
    
        register_post_type(“城市”,$ args);
    
        $ labels = array(
            “名称” => “特性”,
            “ singular_name” => “财产”,
        );
    
        $ args = array(
            “标签” => $标签,
            “描述” => “”,
            “ public” =>真的,
            “ show_ui” =>真的,
            “ has_archive” =>真的,
            “ show_in_menu” =>真的,
            “ dubl_from_search” =>错误的,
            “ caperability_type” => “邮政”,
            “ map_meta_cap” =>真的,
            “层次结构” =>错误的,
            “重写” => array(“ slug” =>“ city/%city_name%”,“ with_front” => true),
            “ query_var” =>真的,
            “支持” =>阵列(“标题”,“修订”,“缩略图”)
        );
    
        register_post_type(“ properties”,$ args);
    
    });
    
    add_action('add_meta_boxes',function(){
        add_meta_box('properties-parent','city',“ properties_attributes_meta_box”,“ properties','side','side','default');
    });
    
    函数properties_attributes_meta_box($ post){
            $ pages = wp_dropdown_pages(array('post_type'=>'city','selected'=> $ post-> post_parent,'name'name'=>'parent_id','show_option_option_none'=&gt'=> __(__(__(')没有父级),'sort_column'=>
            如果(!empty($ pages)){
                回声页;
            } //结束空页检查
    }
    
    add_action('init',function(){
    
        add_rewrite_rule('^city/(.formportect.forcemp/(
    
    });
    
    add_filter('post_type_link',函数($ link,$ post){
        如果('properties'== get_post_type($ post)){
            //让我们去获取父城的名字
            如果($ post-> post_parent){
                $ parent = get_post($ post-> post_parent);
                如果(!empty($ parent-> post_name))
                    返回str_replace('%city_name%',$ parent-> post_name,$ link);
                }
            } 别的 {
                //这似乎不起作用。它打算构建漂亮的永久链接
                //当属性没有父母时,似乎需要
                //其他重写规则
                //返回str_replace('/%city_name%',',$ link);
            }
    
        }
        返回$ link;
    },10,2);
     

You can make your custom posts to have parents like page. So there are three main thing you have to do:

  1. Make you custom post to support Page attributes.

  2. You have to register new rewrite rule, which will then define a new permastruct which will be used in the third step.

  3. After registering the new rewrite rule, you should apply this rule to your custom posts' permalinks.

    Complete working code

    add_action('init', function(){
        $labels = array(
            "name" => "City",
            "singular_name" => "City",
            "menu_name" => "City",
            "all_items" => "All City",
            "add_new" => "Add New",
            "add_new_item" => "Add New City",
            "edit" => "Edit",
            "edit_item" => "Edit City",
            "new_item" => "New City",
            "view" => "View",
            "view_item" => "View City",
            "search_items" => "Search City",
            "not_found" => "No City Found",
            "not_found_in_trash" => "No City Found in Trash",
            "parent" => "Parent City",
        );
    
        $args = array(
            "labels" => $labels,
             "description" => "",
            "public" => true,
            "show_ui" => true,
            "has_archive" => true,
            "show_in_menu" => true,
            "exclude_from_search" => false,
            "capability_type" => "post",
            "map_meta_cap" => true,
            "hierarchical" => true,
            "rewrite" => array( "slug" => "city", "with_front" => true ),
            "query_var" => true,
            "supports" => array( "title", "revisions", "thumbnail" )
        );
    
        register_post_type( "city", $args );
    
        $labels = array(
            "name" => "Properties",
            "singular_name" => "Property",
        );
    
        $args = array(
            "labels" => $labels,
            "description" => "",
            "public" => true,
            "show_ui" => true,
            "has_archive" => true,
            "show_in_menu" => true,
            "exclude_from_search" => false,
            "capability_type" => "post",
            "map_meta_cap" => true,
            "hierarchical" => false,
            "rewrite" => array( "slug" => "city/%city_name%", "with_front" => true ),
            "query_var" => true,
            "supports" => array( "title", "revisions", "thumbnail" )
        );
    
        register_post_type( "properties", $args );
    
    });
    
    add_action('add_meta_boxes', function() {
        add_meta_box('properties-parent', 'City', 'properties_attributes_meta_box', 'properties', 'side', 'default');
    });
    
    function properties_attributes_meta_box($post) {
            $pages = wp_dropdown_pages(array('post_type' => 'city', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __('(no parent)'), 'sort_column'=> 'menu_order, post_title', 'echo' => 0));
            if ( ! empty($pages) ) {
                echo $pages;
            } // end empty pages check
    }
    
    add_action( 'init', function() {
    
        add_rewrite_rule( '^city/(.*)/([^/]+)/?
    
,'index.php?properties=$matches[2]','top' ); }); add_filter( 'post_type_link', function( $link, $post ) { if ( 'properties' == get_post_type( $post ) ) { //Lets go to get the parent city name if( $post->post_parent ) { $parent = get_post( $post->post_parent ); if( !empty($parent->post_name) ) { return str_replace( '%city_name%', $parent->post_name, $link ); } } else { //This seems to not work. It is intented to build pretty permalinks //when properties has not parent, but it seems that it would need //additional rewrite rules //return str_replace( '/%city_name%', '', $link ); } } return $link; }, 10, 2 );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文