Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // See https://wordpress.stackexchange.com/a/306993/137402 for details.
- add_action( 'init', 'my_register_questions_post_types' );
- function my_register_questions_post_types() {
- register_post_type( 'english-speaking', array(
- 'label' => 'English Speaking',
- 'public' => true,
- 'supports' => [ 'title', 'editor', 'custom-fields' ],
- 'rewrite' => array(
- 'slug' => 'english/speaking/%speaking_task%/%question_id%',
- ),
- // Other args here.
- ) );
- register_post_type( 'english-writing', array(
- 'label' => 'English Writing',
- 'public' => true,
- 'supports' => [ 'title', 'editor', 'custom-fields' ],
- 'rewrite' => array(
- 'slug' => 'english/writing/%question_id%',
- ),
- // Other args here.
- ) );
- }
- add_action( 'init', 'my_register_questions_taxonomies' );
- function my_register_questions_taxonomies() {
- register_taxonomy( 'speaking-task', array( 'english-speaking' ), array(
- 'label' => 'Speaking Tasks',
- 'public' => true,
- 'hierarchical' => true,
- 'rewrite' => array(
- 'slug' => 'english/speaking',
- ),
- // Other args here.
- ) );
- }
- add_action( 'init', 'my_register_questions_rewrite_tags' );
- function my_register_questions_rewrite_tags() {
- add_rewrite_tag( '%question_id%', '(\d+)' );
- add_rewrite_tag( '%speaking_task%', '([^/]+)' );
- }
- add_filter( 'post_type_link', 'my_filter_questions_post_type_link', 10, 2 );
- function my_filter_questions_post_type_link( $post_link, $post ) {
- // Replaces/rewrites %question_id% in the permalink.
- if ( false !== strpos( $post_link, '%question_id%' ) ) {
- $id = get_post_meta( $post->ID, 'wpcf-question-id', true );
- // A default value is necessary, and the value has to be a 0.
- $id = $id ? $id : '0';
- $post_link = str_replace( '%question_id%', $id, $post_link );
- }
- // Replaces/rewrites %speaking_task% in the permalink.
- if ( false !== strpos( $post_link, '%speaking_task%' ) ) {
- // A default value is necessary, but the term/category doesn't need to
- // actually exists. So you could, for example, use 'all' as the value.
- $slug = 'uncategorized';
- $cats = get_the_terms( $post, 'speaking-task' );
- if ( $cats && ! is_wp_error( $cats ) ) {
- $slug = $cats[0]->slug;
- }
- $post_link = str_replace( '%speaking_task%', $slug, $post_link );
- }
- return $post_link;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement