Wordpress 一个自定义 post 永久链接结构在另一个自定义 post 永久链接中

Wordpress one custom post permalink structure inside other custom post permalink

这是一个会员网站。

列表(Custom Post Type - 1

团队成员(Custom Post Type - 2

显示单个 post 列表时

然后在该列表的所有团队成员下方显示。

但是当我单击一个团队成员时 post。

然后像这样显示 WordPress 普通永久链接

http://exampol.com/team-member ( custom post type -2 ) / single post link/

用于列表的普通 WordPress 永久链接。

http://exampol.com/ listing ( custom post type -1 ) / single post link/

我想显示这种(下)永久链接。可能吗?

http://exampol.com/ listing ( custom post type -1 ) / single-post-link//team-member ( custom post type -2 ) / single-team-member-link/

我进行了一些研究,发现了这个很酷的主题,我相信它会对您有所帮助。 https://1fix.io/blog/2016/02/05/parent-from-another-cpt/

基本上,我是从文章中引用的,您需要创建 CPT:

    <?php
// Register Custom Post Types
function my_register_cpt() {
    $labels = array(
        'name'                  => _x( 'Courses', 'Post Type General Name', '1fix' ),
        'singular_name'         => _x( 'Course', 'Post Type Singular Name', '1fix' ),
        'menu_name'             => __( 'Courses', '1fix' ),
        'name_admin_bar'        => __( 'Courses', '1fix' )
    );
    $args = array(
        'label'                 => __( 'Course', '1fix' ),
        'labels'                => $labels,
        'hierarchical'          => true,
        'public'                => true
    );
    register_post_type( 'course', $args );
    $labels = array(
        'name'                  => _x( 'Lessons', 'Post Type General Name', '1fix' ),
        'singular_name'         => _x( 'Lesson', 'Post Type Singular Name', '1fix' ),
        'menu_name'             => __( 'Lessons', '1fix' ),
        'name_admin_bar'        => __( 'Lessons', '1fix' )
    );
    $args = array(
        'label'                 => __( 'Lesson', '1fix' ),
        'labels'                => $labels,
        'hierarchical'          => false,
        'public'                => true
    );
    register_post_type( 'lesson', $args );
}
add_action( 'init', 'my_register_cpt' );

然后您需要添加元框以将父 post 类型与子 post 类型相关联:

<?php
function my_add_meta_boxes() {
    add_meta_box( 'lesson-parent', 'Course', 'lesson_attributes_meta_box', 'lesson', 'side', 'high' );
}
add_action( 'add_meta_boxes', 'my_add_meta_boxes' );

function lesson_attributes_meta_box( $post ) {
    $post_type_object = get_post_type_object( $post->post_type );
    $pages = wp_dropdown_pages( array( 'post_type' => 'course', 'selected' => $post->post_parent, 'name' => 'parent_id', 'show_option_none' => __( '(no parent)' ), 'sort_column'=> 'menu_order, post_title', 'echo' => 0 ) );
    if ( ! empty( $pages ) ) {
        echo $pages;
    }
}

之后你需要设置永久链接结构:

<?php
function my_add_rewrite_rules() {
    add_rewrite_tag('%lesson%', '([^/]+)', 'lesson=');
    add_permastruct('lesson', '/lesson/%course%/%lesson%', false);
    add_rewrite_rule('^lesson/([^/]+)/([^/]+)/?','index.php?lesson=$matches[2]','top');
}
add_action( 'init', 'my_add_rewrite_rules' );

最后,更新 post 的永久链接:

<?php
function my_permalinks($permalink, $post, $leavename) {
    $post_id = $post->ID;
    if($post->post_type != 'lesson' || empty($permalink) || in_array($post->post_status, array('draft', 'pending', 'auto-draft')))
        return $permalink;

    $parent = $post->post_parent;
    $parent_post = get_post( $parent );

    $permalink = str_replace('%course%', $parent_post->post_name, $permalink);

    return $permalink;
}
add_filter('post_type_link', 'my_permalinks', 10, 3);