admin管理员组

文章数量:1122832

I am new to wordpress custom post type and re-writing urls'.

I have created a Custom Post Type with slug 'industry' further re-writen to 'industries' and Custom Taxonomy associated to it with same slug 'industries'. The taxonomy archive pages will have nested (hierarchial) pages.

I want following urls' to work:

  • Lists parent taxonomies, for which created custom page template, works fine.
  • Lists child taxonomies, taxonomy-industries.php, works fine.
  • Lists posts, same template taxonomy-industries.php, works fine.
  • Should display post content, instead shows 404 page not found error.

However if I manually try to access single page with taxonomies hierarchy in url, it displays content as it should.

  • /

I want to make this url working for post detail page:

  • /

Here 'automotive-transportation' is parent taxonomy and 'manufacturing-digital-marketing' is post slug.

Here is the code I wrote so far.

//Custom Post Type
function cpt_industry() {
    $args = array(
        'label'             => __( 'Industry', 'sevenc' ),
        'labels'            => array( 'name' => 'Industries', 'singular_name' => 'Industry', ),
        'public'            => true,
        'hierarchical'      => true,
        'has_archive'       => false,
        'show_in_menu'      => true,
        'show_in_admin_bar' => true,
        'menu_position'     => 5,
        'supports'          => array( 'title', 'custom-fields', 'thumbnail', 'editor', 'excerpt'),
        'rewrite'           => array('slug' => 'industries/%industries%/%industries%', 'with_front' => true),
    );
    register_post_type( 'industry', $args );
}
add_action( 'init', 'cpt_industry', 0 );

//Custom Taxonomy - Industries
function ctax_industries() {
    $args = array(
        'labels'            => array( 'name' => 'Industries Categories', 'singular_name' => 'Industry Category', ),
        'show_ui'           => true,
        'show_admin_column' => true,
        'hierarchical'      => true,
        'public'            => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'industries', 'with_front' => true, 'hierarchical' => true ),
    );

    register_taxonomy( 'industries', array( 'industry' ), $args );
}
add_action( 'init', 'ctax_industries', 0 );

//post_type_link() Hook
function custom_post_type_link($post_link, $post) {
    if ($post->post_type == 'industry') {
        $terms = wp_get_object_terms($post->ID, 'industries');
        if ($terms && !is_wp_error($terms)) {
            // Get the parent term
            $parent_term = $terms[0];
            while ($parent_term->parent != 0) {
                $parent_term = get_term($parent_term->parent, 'industries');
            }
            $post_link = str_replace('%industries%/%industries%', $parent_term->slug, $post_link);
        } else {
            $post_link = str_replace('%industries%/%industries%', 'uncategorized', $post_link);
        }
    }
    return $post_link;
}
add_filter('post_type_link', 'custom_post_type_link', 1, 2);

I am trying this since past 2 days, and I am so stuck, any help would be highly appreciated.

Thank you!

本文标签: