0
votes

I am working on a post_type and taxonomy. I create a page template "foo_template.php" for its main page, in main page I write a query that shows all taxonomies and related (5) Five posts. When i click on any taxonomy the new page open and it shows all post titles of clicked taxonomy and its slug is "foo_taxonomy", i also create for it a page "taxonomy-foo_post.php" and when i click any title it goes on single page which i created for it "single-foo_post.php" and its slug is "foo_post".

Now the main issue is i want that post type and taxonomy slug is same when i do this my page layout is disturbed and it goes to archive.php

My friend give me some code i write it, where post_type and taxonomy slug is same, when i click on the taxonomy the page open with new same slug but when i click on the post title for single page it shows that "Page not found"

What is the issue i don't get it.

Here is my code:

Post type and Taxonomy code:

add_action('init', 'foo_articles');
function foo_articles() {
    register_post_type('foo_knowledgebase', array(
        'labels' => array(
            'name' => 'Articles',
            'singular_name' => 'Article'
        ),
        'public' => true,
        'rewrite' => array(
            'slug' => 'My_slug')
    ));
}

add_action( 'init', 'foo_taxonomies', 0 );
function foo_taxonomies() {
    register_taxonomy('foo_taxonomy', array('My_slug'), array(
        'labels' => array(
            'name' => 'Articles Category'
        ),
        'show_ui' => true,
        'show_admin_column' => true,
        'show_tagcloud' => FALSE,
        'hierarchical' => true,
        'rewrite' => array('slug' => 'My_slug', 'with_front' => TRUE)
    ));
}

For same slug code:

$rules = array();
    $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
    $post_types = get_post_types(array('public' => true, '_builtin' => false), 'names');

    foreach ($post_types as $post_type) {
        $post_type_data = get_post_type_object( $post_type );
        $post_type_slug = $post_type_data->rewrite['slug'];

        foreach ($taxonomies as $taxonomy) {
            if ($taxonomy->object_type[0] == $post_type_slug) {
                    $categories = get_categories(array('type' => $post_type_slug, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
                    /* @var $category type */
                    foreach ($categories as $category) {
                        $rules[$post_type_slug . '/' . $category->slug . '/?$'] = 'index.php?' . $category->taxonomy . '=' . $category->slug;
                    }
            }
        }
    }
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter( 'generate_rewrite_rules', 'taxonomy_slug_rewrite' );

My friend said that this code work for him but i dont now what happen here i copied same code.

This condition is also set true

if ($taxonomy->object_type[0] == $post_type_slug)

but i don't know why my slug not work.

Please help me

1

1 Answers

2
votes

For what you want to achieve you will need to define the Custom Taxonomy BEFORE the Custom Post Type. This way will work but in the unlikely event that your Taxonomy and your Post will have the same slug won't be able to view the entry.

e.g. if you will have a taxonomy term called "press" and a CPT with the title "press".