$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Custom post type structure + permalink structure|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Custom post type structure + permalink structure

matteradmin10PV0评论

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?

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far