将类别基础添加到自定义帖子类型/分类中的URL

时间:2013-04-06 作者:Zach Russell

我正在WordPress中构建一个LMS类型的系统,由Custom Post types.
调用post类型Lessons (有一点courses) 它有一个custom taxonomy (类别)调用courses.

域url结构现在显示为:

domain.com/courses/lesson-name.

我希望它成为:

domain.com/courses/[course-name{category}]/lesson-name

或者本质上:

/[cpt]/%category%/%postname%/

这是我写的插件,它控制CPTs 现在

function rflms_post_type() {
    $labels = array(
        \'name\'                => _x( \'Lessons\', \'Post Type General Name\', \'text_domain\' ),
        \'singular_name\'       => _x( \'Lesson\', \'Post Type Singular Name\', \'text_domain\' ),
        \'menu_name\'           => __( \'Lessons\', \'text_domain\' ),
        \'parent_item_colon\'   => __( \'Parent Product:\', \'text_domain\' ),
        \'all_items\'           => __( \'All Lessons\', \'text_domain\' ),
        \'view_item\'           => __( \'View Lesson\', \'text_domain\' ),
        \'add_new_item\'        => __( \'Add New Lesson\', \'text_domain\' ),
        \'add_new\'             => __( \'New Lesson\', \'text_domain\' ),
        \'edit_item\'           => __( \'Edit Lesson\', \'text_domain\' ),
        \'update_item\'         => __( \'Update Lesson\', \'text_domain\' ),
        \'search_items\'        => __( \'Search Lessions\', \'text_domain\' ),
        \'not_found\'           => __( \'No Lessons Found\', \'text_domain\' ),
        \'not_found_in_trash\'  => __( \'No Lessons Found in Trash\', \'text_domain\' ),
    );

    $args = array(
        \'label\'               => __( \'Lessons\', \'text_domain\' ),
        \'description\'         => __( \'Referable Lessons\', \'text_domain\' ),
        \'labels\'              => $labels,
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'supports\'        => array(\'premise-member-access\', \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'comments\'),
        \'menu_position\'       => 5,
        \'menu_icon\'           => null,
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'post\',
        \'rewrite\'                    => array(\'slug\' => \'courses\'),
    );

    register_post_type( \'lessons\', $args );


// Hook into the \'init\' action

}
add_action( \'init\', \'rflms_post_type\', 0 );

// Register Custom Taxonomy
function custom_taxonomy()  {
    $labels = array(
        \'name\'                       => _x( \'Courses\', \'Taxonomy General Name\', \'text_domain\' ),
        \'singular_name\'              => _x( \'Course\', \'Taxonomy Singular Name\', \'text_domain\' ),
        \'menu_name\'                  => __( \'Courses\', \'text_domain\' ),
        \'all_items\'                  => __( \'All Courses\', \'text_domain\' ),
        \'parent_item\'                => __( \'Parent Course\', \'text_domain\' ),
        \'parent_item_colon\'          => __( \'Parent Course:\', \'text_domain\' ),
        \'new_item_name\'              => __( \'New Course Name\', \'text_domain\' ),
        \'add_new_item\'               => __( \'Add New Course\', \'text_domain\' ),
        \'edit_item\'                  => __( \'Edit Course\', \'text_domain\' ),
        \'update_item\'                => __( \'Update Course\', \'text_domain\' ),
        \'separate_items_with_commas\' => __( \'Separate Courses with commas\', \'text_domain\' ),
        \'search_items\'               => __( \'Search Courses\', \'text_domain\' ),
        \'add_or_remove_items\'        => __( \'Add or Remove Courses\', \'text_domain\' ),
        \'choose_from_most_used\'      => __( \'Choose from Most Used courses\', \'text_domain\' ),
    );

    $args = array(
        \'labels\'                     => $labels,
        \'hierarchical\'               => true,
        \'public\'                     => true,
        \'show_ui\'                    => true,
        \'show_admin_column\'          => true,
        \'show_in_nav_menus\'          => true,
        \'show_tagcloud\'              => false,
        \'rewrite\'                    => array(\'slug\' => \'courses\'),
    );

    register_taxonomy( \'course\', \'lessons\', $args );
}

// Hook into the \'init\' action
add_action( \'init\', \'custom_taxonomy\', 0 );

9 个回复
最合适的回答,由SO网友:Milo 整理而成

更改重写以添加课程查询变量:

\'rewrite\' => array(\'slug\' => \'courses/%course%\')
然后筛选post_type_link 要将所选课程插入永久链接,请执行以下操作:

function wpa_course_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, \'course\' );
        if( $terms ){
            return str_replace( \'%course%\' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;  
}
add_filter( \'post_type_link\', \'wpa_course_post_link\', 1, 3 );
还有一些插件,如Custom Post Type Permalinks 可以帮你做到这一点。

SO网友:Floris

我的解决方案有三个部分。在我的例子中,post类型被调用trainings.

添加\'rewrite\' => array(\'slug\' => \'trainings/%cat%\')register_post_type 功能functions.php:

function vx_soon_training_post_link( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if ( is_object( $post ) ) {
        $terms = wp_get_object_terms( $post->ID, \'training_cat\' );
        if ( $terms ) {
            return str_replace( \'%cat%\', $terms[0]->slug, $post_link );
        }
    }

    return $post_link;
}

add_filter( \'post_type_link\', \'vx_soon_training_post_link\', 1, 3 );
。。。这就是如何在新的动态URL上加载适当的模板。添加到functions.php:

function archive_rewrite_rules() {
    add_rewrite_rule(
        \'^training/(.*)/(.*)/?$\',
        \'index.php?post_type=trainings&name=$matches[2]\',
        \'top\'
    );
    //flush_rewrite_rules(); // use only once
}

add_action( \'init\', \'archive_rewrite_rules\' );
就是这样!请记住,通过在de后端再次保存永久链接来刷新永久链接。或使用flush_rewrite_rules() 作用

SO网友:Varsha Dhadge

找到解决方案了!

要使自定义post类型具有层次结构永久链接,请安装自定义post类型永久链接(https://wordpress.org/plugins/custom-post-type-permalinks/) 插件。

更新注册职位类型。我将帖子类型名称作为帮助中心

function help_centre_post_type(){
    register_post_type(\'helpcentre\', array( 
        \'labels\'            =>  array(
            \'name\'          =>      __(\'Help Center\'),
            \'singular_name\' =>      __(\'Help Center\'),
            \'all_items\'     =>      __(\'View Posts\'),
            \'add_new\'       =>      __(\'New Post\'),
            \'add_new_item\'  =>      __(\'New Help Center\'),
            \'edit_item\'     =>      __(\'Edit Help Center\'),
            \'view_item\'     =>      __(\'View Help Center\'),
            \'search_items\'  =>      __(\'Search Help Center\'),
            \'no_found\'      =>      __(\'No Help Center Post Found\'),
            \'not_found_in_trash\' => __(\'No Help Center Post in Trash\')
                                ),
        \'public\'            =>  true,
        \'publicly_queryable\'=>  true,
        \'show_ui\'           =>  true, 
        \'query_var\'         =>  true,
        \'show_in_nav_menus\' =>  false,
        \'capability_type\'   =>  \'page\',
        \'hierarchical\'      =>  true,
        \'rewrite\'=> [
            \'slug\' => \'help-center\',
            "with_front" => false
        ],
        "cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",
        \'menu_position\'     =>  21,
        \'supports\'          =>  array(\'title\',\'editor\', \'thumbnail\'),
        \'has_archive\'       =>  true
    ));
    flush_rewrite_rules();
}
add_action(\'init\', \'help_centre_post_type\');
这是注册分类法

function themes_taxonomy() {  
    register_taxonomy(  
        \'help_centre_category\',  
        \'helpcentre\',        
        array(
            \'label\' => __( \'Categories\' ),
            \'rewrite\'=> [
                \'slug\' => \'help-center\',
                "with_front" => false
            ],
            "cptp_permalink_structure" => "/%help_centre_category%/",
            \'hierarchical\'               => true,
            \'public\'                     => true,
            \'show_ui\'                    => true,
            \'show_admin_column\'          => true,
            \'show_in_nav_menus\'          => true,
            \'query_var\' => true
        ) 
    );  
}  
add_action( \'init\', \'themes_taxonomy\');
这条线让你的permalink工作

"cptp_permalink_structure" => "/%help_centre_category%/%post_id%-%postname%/",
您可以删除%post_id% 并且可以保持/%help_centre_category%/%postname%/"

别忘了冲洗仪表板上的永久链接。

SO网友:Chetan Vaghela

您需要使用register\\u post\\u type函数在下面的一行中更新您已经注册了自定义post类型的位置。

\'rewrite\' => array(\'slug\' => \'courses/%cat%\')

要动态更改post类型的permalink,必须在函数中添加以下代码。php文件:

function change_link( $post_link, $id = 0 ) {
    $post = get_post( $id );
    if( $post->post_type == \'courses\' ) 
    {
       if ( is_object( $post ) ) {
          $terms = wp_get_object_terms( $post->ID, array(\'course\') );
          if ( $terms ) {
             return str_replace( \'%cat%\', $terms[0]->slug, $post_link );
         }
      }
    }
    return   $post_link ;
}
add_filter( \'post_type_link\', \'change_link\', 1, 3 );

//load the template on the new generated URL otherwise you will get 404\'s the page

function generated_rewrite_rules() {
   add_rewrite_rule(
       \'^courses/(.*)/(.*)/?$\',
       \'index.php?post_type=courses&name=$matches[2]\',
       \'top\'
   );
}
add_action( \'init\', \'generated_rewrite_rules\' );
之后,需要刷新重写永久链接,转到wp-admin > Settings > permalinks. 只需使用“保存更改”按钮更新永久链接设置。

它将返回如下URL:

域。com/courses/[课程名称{类别}]/课程名称谢谢!

SO网友:maheshwaghmare

Yep! 经过大量研究,我得到了插件\'Custom Permalinks\'. 这满足了我的要求-自定义URL,例如。

类别为Post,自定义Post,自定义分类法Custom Post Type - Post:

enter image description here

SO网友:Malki Mohamed

这对我有用:

\'rewrite\' => array(
        \'slug\' => \'portfolio\',
        \'with_front\' => false,
        \'hierarchical\' => true // to display category/subcategroy
    ),

SO网友:Khom Nazid

对于任何对该解决方案感兴趣的人,无需修改原始PHP代码,我强烈推荐该插件Permalink Manager Lite

它有一种可视机制,可以根据“Per马斯tructs”在自定义帖子类型的URL中删除或添加任何您想要的部分:

Screenshot of Permalink Manager Lite

(由于使用自定义帖子类型进行简单的URL结构所带来的种种痛苦,我们打算放弃WP,转而使用另一个CMS。但这个插件与ACF、CPTUI或Pods结合使用,使得Wordpress相当专业。)

SO网友:marcelo2605

如果您正在使用get_post_type_archive_link(), 也许你需要删除/%cat%/ 从URL使用post_type_archive_link 滤器

SO网友:EliT

我发现@ChetanVaghela的答案近乎完美;在我的用例中,我还希望能够按此帖子类型查看所有帖子的列表,就像一个典型的归档页面(即/courses/,后面没有任何分类)。我只需添加一条额外的重写规则,如下所示:

function generated_rewrite_rules() {
    add_rewrite_rule(
        \'^courses/(.*)/(.*)/?$\',
        \'index.php?post_type=courses&name=$matches[2]\',
        \'top\'
    );
}

结束

相关推荐