Wordpress Costum帖子类型 - URL中的ACF

时间:2016-12-14 16:07:48

标签: php wordpress custom-post-type advanced-custom-fields

我有一些问题,我需要帮助 -

我注册了costum post type - slug是“project”

但我需要制作3个类别(使用ACF)并因此更改网址

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

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

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

如何在没有插件的情况下制作它??

感谢帮助者:)

1 个答案:

答案 0 :(得分:0)

经过测试和验证:

我正在逐步写清楚:

  1. 确保您在注册自定义帖子类型rewrite时获取更多信息:register custom post type
  2. 'rewrite' => array('slug' => '/%project_categories%','with_front' => FALSE),

    1. 然后注册您的分类 project_categories
    2. 您需要为此自定义(项目)帖子类型注册自定义分类 project_categories register_taxonomyread here了解更多

      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. 上次制作时,您的帖子类型会过滤。

        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);
        
      2. 注意:如果您发现帖子未找到错误。然后将永久链接设置更改为默认并保存更改。并再次设置永久链接到帖子名称,它将工作。