【问题标题】:Wordpress Costum Post Type - ACF in URLWordpress 自定义帖子类型 - URL 中的 ACF
【发布时间】:2016-12-14 16:07:47
【问题描述】:

我有一些问题需要他们帮助 -

我注册了服装帖子类型 - slug 是“项目”

但我需要创建 3 个类别(使用 ACF)并因此更改 url

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

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

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

没有插件我怎样才能做到...?

感谢帮助:)

【问题讨论】:

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


    【解决方案1】:

    经过测试和验证:

    我正在逐步写清楚:

    1. 请确保您在注册自定义帖子类型时rewrite 了解更多信息:register custom post type

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

    1. 然后注册您的分类法project_categories

    您需要为此自定义(项目)帖子类型注册自定义分类project_categories。 register_taxonomy请read 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. Last make,您的帖子类型喜欢过滤器。

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

    注意:如果您看到未找到帖子错误。然后将您的永久链接设置更改为 default 并保存更改。并再次将永久链接设置为 Post name,它将起作用。

    【讨论】:

    • 嘿 - 感谢您的快速评论,但我如何为每个帖子选择分类法?
    • 您需要在后台添加您的 3 个类别,并在您喜欢普通帖子中的类别时分配给这些帖子
    • 我看到在后端选择的类别,但它并没有改变 url...所有帖子仍然是 /project/name-of-project
    • 转到您的永久链接设置并添加/%category%/%postname%
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-18
    • 2017-02-28
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多