dousi4900 2016-12-14 16:07
浏览 43

Wordpress自定义帖子类型 - URL中的ACF

I have some problem that I need help in them -

I register costum post type - is slug is "project"

But I need to make 3 categories (with ACF) and make url changed due to it

category1 = http://www.example.com/mycaturl/nameoftitle

category2 = http://www.example.com/othercat/nameoftitle

category3 = http://www.example.com/customedit/nameoftitle

How can I make it without plugins...?

Thanks for helpers :)

  • 写回答

1条回答 默认 最新

  • douwen3500 2016-12-14 16:14
    关注

    Tested and verified:

    I am writting in steps to make it clear:

    1. Make sure when you registered your custom post type rewrite for more: register custom post type

    'rewrite' => array('slug' => '/%project_categories%','with_front' => FALSE),

    1. Then register your taxonomies project_categories

    You need to register custom taxonomy project_categories for this custom (project) post type. register_taxonomy please read here for more

    function project_taxonomy() {  
        register_taxonomy(  
            'project_categories',  //The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces). 
            'project',        //post type name
            array(  
                'hierarchical' => true,  
                'label' => 'project store',  //Display name
                'query_var' => true,
                'rewrite' => array(
                    'slug' => 'project', // This controls the base slug that will display before each term
                    'with_front' => false //  Don't display the category base before 
                )
            )  
        );  
    }  
    add_action( 'init', 'project_taxonomy');
    
    1. Last make, your post type liks filters.

      function filter_post_type_link($link, $post)
       {
          if ($post->post_type != 'project')
              return $link;
      
          if ($cats = get_the_terms($post->ID, 'project_categories'))
              $link = str_replace('%project_categories%', array_pop($cats)->slug, $link);
          return $link;
      }
      add_filter('post_type_link', 'filter_post_type_link', 10, 2);
      

    Note: if you see post not found error. then change your permalink settings to default and save changes. And again set permalink to Post name and it will work.

    评论

报告相同问题?