【问题标题】:How to Add Multiple Taxonomies in Custom Post Permalink in WordPress URL structure如何在 WordPress URL 结构中的自定义帖子固定链接中添加多个分类法
【发布时间】:2020-07-17 11:36:17
【问题描述】:

我创建了一个名为“企业列表”的自定义帖子类型,其中包含多种分类法(“专业、州、市”) 例如: 自定义帖子名称:Dr. Jones Dental 分类学(专业):普通牙医 分类学(州):加利福尼亚 分类(城市):帕萨迪纳

所以目前,网址如下所示: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 重定向的情况下实现这一目标呢?

【问题讨论】:

标签: php wordpress custom-post-type custom-taxonomy


【解决方案1】:

我给你我在你的情况下使用的代码


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

【讨论】:

  • 嗨@Dharman,感谢您的帮助。但是我从这篇文章中得到了我想要的结果wisdmlabs.com/blog/…
猜你喜欢
  • 2012-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-05
  • 2017-11-27
  • 2021-06-10
相关资源
最近更新 更多