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
Php 如何在WordPress URL结构中的自定义Post Permalink中添加多个分类法_Php_Wordpress_Custom Post Type_Custom Taxonomy - Fatal编程技术网

Php 如何在WordPress URL结构中的自定义Post Permalink中添加多个分类法

Php 如何在WordPress URL结构中的自定义Post Permalink中添加多个分类法,php,wordpress,custom-post-type,custom-taxonomy,Php,Wordpress,Custom Post Type,Custom Taxonomy,我已经创建了一个名为Business Listing的自定义帖子类型,其中包含多种分类(“专业、州、市”) 例如: 定制职位名称:琼斯牙科医生 分类学(专业):普通牙医 分类法(州):加利福尼亚州 分类学(城市):帕萨迪纳 因此,当前的url如下所示: 这是我想要的URL结构,应该是: 然而,我需要得到一个完美的URL结构,是搜索引擎优化友好。因此,最终的URL结构应该如下所示: . 那么,我如何在不使用301重定向的情况下实现这一点呢?我将为您提供我在您的案例中使用的代码 欢迎来到Stac

我已经创建了一个名为Business Listing的自定义帖子类型,其中包含多种分类(“专业、州、市”) 例如: 定制职位名称:琼斯牙科医生 分类学(专业):普通牙医 分类法(州):加利福尼亚州 分类学(城市):帕萨迪纳

因此,当前的url如下所示:

这是我想要的URL结构,应该是:

然而,我需要得到一个完美的URL结构,是搜索引擎优化友好。因此,最终的URL结构应该如下所示: .
那么,我如何在不使用301重定向的情况下实现这一点呢?

我将为您提供我在您的案例中使用的代码



欢迎来到StackOverflow。为防止投票被否决或结束,请检查并编辑您的问题以满足此要求。如果没有证据表明有研究或自己解决问题的良好尝试,或者没有包括:错误的具体细节、您迄今为止尝试过的内容以及a中的相关代码,则问题可能会被否决。您好@Dharman,谢谢您的帮助。然而,我从这篇文章中得到了我想要的结果
class BusinesListing
{


    public function __construct()
    {
        // CPT
        add_action('init', [
            $this,
            'create_busines_listing_cpt'
        ], 0);

        // TAXOS
        add_action('init', [
            $this,
            'create_taxo_speciality'
        ]);
        add_action('init', [
            $this,
            'create_taxo_state'
        ]);
        add_action('init', [
            $this,
            'create_taxo_city'
        ]);

        // URLs
        add_filter('post_type_link', [
            $this,
            'custom_permalink'
        ], 1, 2);
    }

    public function create_busines_listing_cpt()
    {
        register_post_type('busines_listing', [
            // ...
            'taxonomies' => array(
                'taxo_speciality',
                'taxo_state',
                'taxo_city'
            ),
            'public' => true,
            'rewrite' => array(
                'slug' => 'busines-listing/%taxo_speciality%/%taxo_state%/%taxo_city%',
                'with_front' => true
            ),
            // ...
        ]);
    }
    public function create_taxo_speciality()
    {
        register_taxonomy('taxo_speciality', ['busines_listing'], [
            // ...
            'label' => 'speciality',
            'rewrite'=>  array(
                'slug' => 'busines-listing',
                'with_front' => true
            ),
            // ...
            ]
        );
    }
    public function create_taxo_state()
    {
        register_taxonomy('taxo_state', ['busines_listing'], [
            // ...
            'label' => 'state',
            'rewrite'=>  array(
                'slug' => 'busines-listing/%taxo_speciality%',
                'with_front' => true
            ),
            // ...
            ]
        );
    }
    public function create_taxo_city()
    {
        register_taxonomy('taxo_city', ['busines_listing'], [
            // ...
            'label' => 'city',
            'rewrite'=>  array(
                'slug' => 'busines-listing/%taxo_speciality%/%taxo_state%',
                'with_front' => true
            ),
            // ...
            ]
        );
    }

    public function custom_permalink($post_link, $post)
    {
        // url
        if (is_object($post) && $post->post_type == 'busines_listing') {
            $terms = wp_get_object_terms($post->ID, 'taxo_speciality');
            if (! empty($terms)) {
                $post_link =  str_replace('%taxo_speciality%', $terms[0]->slug, $post_link);
            }
            $terms = wp_get_object_terms($post->ID, 'taxo_state');
            if (! empty($terms)) {
                $post_link =  str_replace('%taxo_state%', $terms[0]->slug, $post_link);
            }
            $terms = wp_get_object_terms($post->ID, 'taxo_city');
            if (! empty($terms)) {
                $post_link =  str_replace('%taxo_city%', $terms[0]->slug, $post_link);
            }
        }
        return $post_link;
    }
}
new BusinesListing();