Skip to content

Instantly share code, notes, and snippets.

@hnq90
Forked from yoren/functions.php
Created January 11, 2018 16:52
Show Gist options
  • Save hnq90/d9d63414096e24afb9d3d9abc1da77ed to your computer and use it in GitHub Desktop.
Save hnq90/d9d63414096e24afb9d3d9abc1da77ed to your computer and use it in GitHub Desktop.
Parent Post From Another Post Type And A New URL Structure In WordPress
<?php
// Flush rewrite rules after switch theme
function my_rewrite_flush() {
flush_rewrite_rules();
}
add_action( 'after_switch_theme', 'my_rewrite_flush' );
// A little help so we can get the stylesheet from parent theme
// Remove line 10-19 if this is not a child theme
function my_enqueue_styles() {
$parent_style = 'parent-style';
wp_enqueue_style( $parent_style, get_template_directory_uri() . '/style.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/style.css',
array( $parent_style )
);
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_styles' );
// 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' );
// Add our Course parent meta box
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' );
// Get the Course dropdown
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;
}
}
// Add our own URL strucutre and rewrite rules
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' );
// Set permalink for Lessons
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);
<?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
// 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' );
<?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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment