Adding Taxonomy Name between Custom Post Type and post
-
Hi Guys
This is a continuation of a discussion started here: http://wordpress.org/support/topic/insert-category-of-taxonomy-between-custom-post-type-and-post and continued here: https://wordpress.org/support/topic/adding-taxonomy-name-to-custom-post-type-and-retain-pagination
The full post of what I’m trying to do is here https://wordpress.org/support/topic/taxonomy-template-hierarchy-link-to-archive-page-help-needed but in a nutshell:
I want to set a permalink structure up so:
http://localhost/wordpress/recipes/ -> Links to archive-recipe.php, which shows latest recipes of all types (custom post type recipes, taxonomy recipetypes). This is working
http://localhost/wordpress/recipes/meals/ -> Links to archive-recipe.php and passes the term “meals” so only recipes of the taxonomy term “meals” are shown but I can use the same page, archive-recipe.php
http://localhost/wordpress/recipes/meals/mac-and-cheese -> Links to single-recipe.php, which shows a single recipe post.
I’m currently using (just the relevant bits):
// Set other options for Recipes Custom Post Type $args = array( 'supports' => array( 'title', 'excerpt', 'author', 'thumbnail', 'revisions'), 'rewrite' => array( 'slug' => 'recipes'), 'taxonomies' => array( 'recipetypes' ), 'hierarchical' => false, 'public' => true, 'can_export' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post' ); // Register the Recipes Custom Post Type register_post_type( 'recipe', $args ); register_taxonomy('recipetypes', 'recipe', array( 'hierarchical' => false, 'rewrite' => array( 'slug' => 'recipes' ) ) ));This works for http://localhost/wordpress/recipes/ (archive-recipe.php) and shows individual recipes (i.e. http://localhost/wordpress/recipes/mac-and-cheese/) but the URL structure is not quite there (missing the term). If I use lopinsjk’s solution I can get this: http://localhost/wordpress/recipes/meals/mac-and-cheese and this: http://localhost/wordpress/recipes/meals/ (but this links to index.php not archive-recipe.php)
But this now gives me a 404: http://localhost/wordpress/recipes/
Here’s what I tried using lopinsjk’s code (just the relevant bits):
$args = array( 'rewrite' => array( 'slug' => 'recipes/%recipetypes%','with_front' => false), 'taxonomies' => array( 'recipetypes' ), 'hierarchical' => false, 'public' => true, 'has_archive' => true, 'exclude_from_search' => false, 'publicly_queryable' => true, 'capability_type' => 'post' ); // Register the Recipes Custom Post Type register_post_type( 'recipe', $args ); register_taxonomy('recipetypes', 'recipe', array( 'hierarchical' => false, 'query_var' => 'recipetypes', 'rewrite' => array( 'slug' => 'recipes' ) ); add_filter('post_link', 'recipetypes_permalink', 1, 3); add_filter('post_type_link', 'recipetypes_permalink', 1, 3); function recipetypes_permalink($permalink, $post_id, $leavename) { if (strpos($permalink, '%recipetypes%') === FALSE) return $permalink; // Get post $post = get_post($post_id); if (!$post) return $permalink; // Get taxonomy terms $terms = wp_get_object_terms($post->ID, 'recipetypes'); if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug; else $taxonomy_slug = 'no-brand'; return str_replace('%recipetypes%', $taxonomy_slug, $permalink); }What do I need to do to get http://localhost/wordpress/recipes/meals/ and http://localhost/wordpress/recipes/ to link to archive-recipe.php and http://localhost/wordpress/recipes/meals/mac-and-cheese/ to link to single-recipe.php?
Many thanks in advance 🙂
The topic ‘Adding Taxonomy Name between Custom Post Type and post’ is closed to new replies.