Hello
I am working on my local server, and the folder name for my WordPress is “custom_theme.” I am currently testing the following code. However, it is not functioning as expected. When I add a lesson, like “simple lesson,” the resulting URL is https://localhost/custom_theme/simple-lesson/, leading to a 404 error.
I am trying to figure out why it is not working as intended. Could you please assist me in understanding the reason behind this issue? Additionally, if I choose a category, such as “California,” the expected URL should be https://localhost/custom_theme/california/simple-lesson/.
// START: CPT ‘Lessons’
function register_lessons_post_type() {
$labels = array(
‘name’ => _x( ‘Lessons’, ‘post type general name’, ‘essentials’ ),
‘singular_name’ => _x( ‘Lesson’, ‘post type singular name’, ‘essentials’ ),
‘menu_name’ => _x( ‘Lessons’, ‘admin menu’, ‘essentials’ ),
‘name_admin_bar’ => _x( ‘Lesson’, ‘add new on admin bar’, ‘essentials’ ),
‘add_new’ => _x( ‘Add New’, ‘lesson’, ‘essentials’ ),
‘add_new_item’ => ( ‘Add New Lesson’, ‘essentials’ ), ‘new_item’ => ( ‘New Lesson’, ‘essentials’ ),
‘edit_item’ => ( ‘Edit Lesson’, ‘essentials’ ), ‘view_item’ => ( ‘View Lesson’, ‘essentials’ ),
‘all_items’ => ( ‘All Lessons’, ‘essentials’ ), ‘search_items’ => ( ‘Search Lessons’, ‘essentials’ ),
‘not_found’ => ( ‘No lessons found’, ‘essentials’ ), ‘not_found_in_trash’ => ( ‘No lessons found in Trash’, ‘essentials’ ),
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
// 'rewrite' => array( 'slug' => 'lessons' ),
'rewrite' => array( 'slug' => 'lesson', 'with_front' => false ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ),
);
register_post_type( 'lessons', $args );
}
add_action( ‘init’, ‘register_lessons_post_type’ );
function custom_theme_lessons_permalink( $post_link, $post ) {
if ( is_object( $post ) && $post->post_type == ‘lessons’ ) {
$post_link = str_replace( ‘/lesson/’, ‘/’, $post_link );
}
return $post_link;
}
add_filter( ‘post_type_link’, ‘custom_theme_lessons_permalink’, 10, 2 );
function register_lessons_taxonomy() {
$labels = array(
‘name’ => x( ‘Lesson Categories’, ‘taxonomy general name’, ‘essentials’ ), ‘singular_name’ => _x( ‘Lesson Category’, ‘taxonomy singular name’, ‘essentials’ ), ‘search_items’ => ( ‘Search Lesson Categories’, ‘essentials’ ), ‘all_items’ => ( ‘All Lesson Categories’, ‘essentials’ ), ‘parent_item’ => ( ‘Parent Lesson Category’, ‘essentials’ ), ‘parent_item_colon’ => ( ‘Parent Lesson Category:’, ‘essentials’ ), ‘edit_item’ => ( ‘Edit Lesson Category’, ‘essentials’ ), ‘update_item’ => ( ‘Update Lesson Category’, ‘essentials’ ), ‘add_new_item’ => ( ‘Add New Lesson Category’, ‘essentials’ ), ‘new_item_name’ => ( ‘New Lesson Category Name’, ‘essentials’ ), ‘menu_name’ => _( ‘Lesson Categories’, ‘essentials’ ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'lesson-category' ),
);
register_taxonomy( 'lesson_category', array( 'lessons' ), $args );
}
add_action( ‘init’, ‘register_lessons_taxonomy’ );
function custom_theme_category_permalink( $termlink, $term, $taxonomy ) {
if ( ‘lesson-category’ == $taxonomy ) {
$termlink = str_replace( ‘/lesson-category/’, ‘/’, $termlink );
}
return $termlink;
}
add_filter( ‘category_link’, ‘custom_theme_category_permalink’, 10, 3 );