Custom post type structure + permalink structure

admin2025-06-07  9

I need to create this structure:

  • example/animals (with all animals classes)
  • example/animals/classes (with all animals of this classes)
  • example/animals/classes/animal_name (the animal's page)

I doubt that the following solution is correct:

I created a Custom post type "animals" with this rule 'rewrite' => array('slug' => 'animals/%classes%') where classes is my custom taxonomy.

And I added this filter to make the rewrite work.

function addTaxonomyInUrl($post_link, $id = 0) {
    $post = get_post($id);
    if(is_object($post) ) {
        $terms = wp_get_object_terms($post->ID,'classes');
        if($terms) { 
            return str_replace('%classes%', $terms[0]->slug, $post_link);
        }
    }
    return $post_link;
}
add_filter ('post_type_link','addTaxonomyInUrl',1,3);

This works well except for two things:

1 - the page example/animals, I tried to create it as an archive temaplate of animals but dosen't work. (for the moment I workarounded the problem using a normale page)

2 - the rewite of animal's pages, example/animals/reptiles/python works well but if I change "reptiles" with something else ex. example/animals/foo/python I keep on seeing the Python page instead of a 404 error.

Could you plesse give me some suggestions?

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

最新回复(0)