如何在 WordPress URL 结构中的自定义 Post 永久链接中添加多个分类法

How to Add Multiple Taxonomies in Custom Post Permalink in WordPress URL structure

我创建了一个自定义 post 类型,名为 Business Listing,具有多个分类法(“专业、州、城市”) 例如: 自定义 post 姓名:Jones Dental 博士 分类(专业):普通牙医 分类(州):加利福尼亚州 分类(城市):帕萨迪纳

所以目前,url 看起来像这样:https://test.com/business-listing/dr-jones-dental

这是我想要的 URL 结构应该如下所示: https://test.com/business-listing/general-dentist/california/pasadena/dr-jones-dental

但是,我需要获得一个对 SEO 友好的完美 URL 结构。所以,最终的 URL 结构应该是这样的: https://test.com/general-dentist/california/pasadena/。 那么如何在不使用 301 重定向的情况下实现这一目标呢?

我给你我在你的案例中使用的代码


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();