plugin development - Permalinks: custom post type -> custom taxonomy -> custom sub taxonomy -> post

admin2025-01-07  6

I am building a custom post type and its respective custom taxonomy.

Here is the code:

add_action( 'init', 'register_my_types' );

function register_my_types() {

    register_post_type( 'help',
        array(
            'labels' => array(
                'name' => __( 'Help Manager' ),
                'singular_name' => __( 'Help' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array(
                'slug' => 'help/%help_categories%',
                'with_front' => true
              )
        )
    );

    register_taxonomy( 'help_categories', array( 'help' ), array( 
            'hierarchical' => true, 
            'label' => 'Help Categories',
            'rewrite' => array(
                'slug' => 'help',
                'with_front' => true
              )
        )
    );
}

I am the doing a rewrite to include this in one pretty URL:

function so23698827_add_rewrite_rules( $rules ) {
    $new = array();
    $new['help/([^/]+)/(.+)/?$'] = 'index.php?help=$matches[2]';
    $new['help/(.+)/?$'] = 'index.php?help_categories=$matches[1]';

    return array_merge( $new, $rules ); // Ensure our rules come first
  }
  add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

  /**
   * Handle the '%project_category%' URL placeholder
   *
   * @param str $link The link to the post
   * @param WP_Post object $post The post object
   * @return str
   */
  function so23698827_filter_post_type_link( $link, $post ) {
    if ( $post->post_type == 'help' ) {
      if ( $cats = get_the_terms( $post->ID, 'help_categories' ) ) {
        $link = str_replace( '%help_categories%', current( $cats )->slug, $link );
      }
    }
    return $link;
  }
  add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );

I want to include any sub category in the URL. So for example, I have the following structure:

  • Custom Post Type
    • Main Category
      • Sub Category
        • Sample Post

The URL structure should be the following www.domain/custom_post_type/main-category/sub-category/sample-post

Can you help me achieve the above please

I am building a custom post type and its respective custom taxonomy.

Here is the code:

add_action( 'init', 'register_my_types' );

function register_my_types() {

    register_post_type( 'help',
        array(
            'labels' => array(
                'name' => __( 'Help Manager' ),
                'singular_name' => __( 'Help' )
            ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array(
                'slug' => 'help/%help_categories%',
                'with_front' => true
              )
        )
    );

    register_taxonomy( 'help_categories', array( 'help' ), array( 
            'hierarchical' => true, 
            'label' => 'Help Categories',
            'rewrite' => array(
                'slug' => 'help',
                'with_front' => true
              )
        )
    );
}

I am the doing a rewrite to include this in one pretty URL:

function so23698827_add_rewrite_rules( $rules ) {
    $new = array();
    $new['help/([^/]+)/(.+)/?$'] = 'index.php?help=$matches[2]';
    $new['help/(.+)/?$'] = 'index.php?help_categories=$matches[1]';

    return array_merge( $new, $rules ); // Ensure our rules come first
  }
  add_filter( 'rewrite_rules_array', 'so23698827_add_rewrite_rules' );

  /**
   * Handle the '%project_category%' URL placeholder
   *
   * @param str $link The link to the post
   * @param WP_Post object $post The post object
   * @return str
   */
  function so23698827_filter_post_type_link( $link, $post ) {
    if ( $post->post_type == 'help' ) {
      if ( $cats = get_the_terms( $post->ID, 'help_categories' ) ) {
        $link = str_replace( '%help_categories%', current( $cats )->slug, $link );
      }
    }
    return $link;
  }
  add_filter( 'post_type_link', 'so23698827_filter_post_type_link', 10, 2 );

I want to include any sub category in the URL. So for example, I have the following structure:

  • Custom Post Type
    • Main Category
      • Sub Category
        • Sample Post

The URL structure should be the following www.domain.com/custom_post_type/main-category/sub-category/sample-post

Can you help me achieve the above please

Share Improve this question edited Feb 3, 2019 at 14:00 Jojo asked Jan 30, 2019 at 12:09 JojoJojo 113 bronze badges 7
  • If you've already managed to get the custom post type and custom taxonomy up to speed, the problem/challenge seems to be with the permalinks. I suggest looking there. You can find these settings in the Wordpress Dashboard > Settings > Permalinks. – Bram Commented Jan 30, 2019 at 12:36
  • are you getting 404 error? – Chinmoy Kumar Paul Commented Jan 30, 2019 at 12:40
  • I wish permalinks was the problem. It's not. I am not able to get the custom post type and the custom taxonomy to work under the structure I explained above – Jojo Commented Jan 30, 2019 at 13:08
  • @ChinmoyKumarPaul - Its not about a 404 its about not managing to get the URL structure combined between the CPT and the taxonomy – Jojo Commented Jan 30, 2019 at 13:09
  • I am getting same kind of posts here wordpress.stackexchange.com/questions/53515/… wordpress.stackexchange.com/questions/94817/… Check them properly – Chinmoy Kumar Paul Commented Jan 30, 2019 at 13:14
 |  Show 2 more comments

1 Answer 1

Reset to default 0

you can try with this plugin (or see its code)

https://wordpress.org/plugins/wp-better-permalinks/

It allows you change the custom friendly permalinks structure to Custom Post Type > Taxonomy > Post and Custom Post Type > Taxonomy instead of default WordPress structure. it Provides different permalinks structure pattern: Custom Post Type > Single Term (or Term tree) > Post Custom Post Type > Post (when no term is selected) Custom Post Type > Single Term (or Term tree)

It helped me a lot with this issue...

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260401a663.html

最新回复(0)