[英]Wordpress one custom post permalink structure inside other custom post permalink
It's a Membership website.这是一个会员网站。
Listing ( Custom Post Type - 1
)列表( Custom Post Type - 1
)
Team Member ( Custom Post Type - 2
)团队成员( Custom Post Type - 2
)
When showing a single post for a listing显示列表的单个帖子时
Then showing below all Team members for this listing.然后在此列表的所有团队成员下方显示。
But when I click a Team Member for a single post.但是当我点击一个团队成员的单个帖子时。
Then show WordPress Normal Permalink like this然后像这样显示 WordPress 普通永久链接
http://exampol.com/team-member ( custom post type -2 ) / single post link/
Normal WordPress permalink for listing.正常 WordPress 永久链接用于列表。
http://exampol.com/ listing ( custom post type -1 ) / single post link/
I want to show this type(Below) of permalink.我想展示这种类型(下)的永久链接。 Can it possible?有可能吗?
http://exampol.com/ listing ( custom post type -1 ) / single-post-link//team-member ( custom post type -2 ) / single-team-member-link/
I've conducted some research and found out this cool topic which I believe it will help you.我进行了一些研究,发现了这个很酷的话题,我相信它会对你有所帮助。 https://1fix.io/blog/2016/02/05/parent-from-another-cpt/ https://1fix.io/blog/2016/02/05/parent-from-another-cpt/
Basically, and I'm quoting from the article, you need to create the CPTs:基本上,我从文章中引用,您需要创建 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' );
And then you need to add meta box to relate the parent post type to the child post type:然后您需要添加元框以将父帖子类型与子帖子类型相关联:
<?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;
}
}
After that you need to set the permalink structure:之后,您需要设置永久链接结构:
<?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' );
And finally, update the permalink of the 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.