Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialgerardo barroso
42,674 PointsLoad single template via plugin on custom url structure
Hello,
Im creating a photo gallery plugin which will be using custom post types and taxonomy templates.
The url structure I want is the following.
domain.com/post-type/taxonomy/post-name
I was able to load the taxonomy template via plugin.
Im using this code to load the templates via plugin depending on the page.
function pss_load_templates( $original_template ) {
if ( is_tax('plugin-procedure')) {
if ( file_exists( get_stylesheet_directory(). 'taxonomy-plugin-procedure.php' ) ) {
return get_stylesheet_directory() . 'taxonomy-plugin-procedure.php';
} else {
return plugin_dir_path(__FILE__) . 'taxonomy-plugin-procedure.php';
}
} elseif(is_single('plugin-gallery')) {
if ( file_exists( get_stylesheet_directory(). 'single-plugin-gallery.php' ) ) {
return get_stylesheet_directory() . 'single-plugin-gallery.php';
} else {
return plugin_dir_path(__FILE__) . 'single-plugin-gallery.php';
}
}
return $original_template;
}
add_action( 'template_include', 'pss_load_templates' );
The problem.
I can't load the single page template using the custom url structure. If I use wordpress default url structure it works fine.
This is my custom post type and rewrites
/**
* Tell WordPress how to interpret our project URL structure
*
* @param array $rules Existing rewrite rules
* @return array
*/
function pss_add_rewrite_rules( $rules ) {
$new = array();
$new['plugin-gallery/([^/]+)/page/?([0-9]{1,})/?$'] = 'index.php?plugin-procedure=$matches[1]&paged=$matches[2]';
return array_merge( $new, $rules ); // Ensure our rules come first
}
add_filter( 'rewrite_rules_array', 'pss_add_rewrite_rules' );
/**
* Handle the '%procedures%' URL placeholder
*
* @param str $link The link to the post
* @param WP_Post object $post The post object
* @return str
*/
function pss_filter_post_type_link( $link, $post ) {
if ( $post->post_type == 'plugin-gallery' ) {
if ( $cats = get_the_terms( $post->ID, 'plugin-procedure' ) ) {
$link = str_replace( '%procedures%', current( $cats )->slug, $link );
}
}
return $link;
}
add_filter( 'post_type_link', 'pss_filter_post_type_link', 10, 2 );
/* ====================
Post Type: Gallery
==================== */
add_action('init', 'plugin_register_my_cpt_gallery');
function plugin_register_my_cpt_gallery() {
$singular = __( 'Plugin Gallery' );
$plural = __( 'Plugin Galleries' );
//Used for the rewrite slug below.
$singular_slug = str_replace( ' ', '-', $singular );
//Setup all the labels to accurately reflect this post type.
$labels = array(
'name' => $singular,
'singular_name' => $singular,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $singular,
'edit' => 'Edit',
'edit_item' => 'Edit ' . $singular,
'new_item' => 'New ' . $singular,
'view' => 'View ' . $singular,
'view_item' => 'View ' . $singular,
'search_term' => 'Search ' . $plural,
'parent' => 'Parent ' . $singular,
'not_found' => 'No ' . $plural .' found',
'not_found_in_trash' => 'No ' . $plural .' in Trash'
);
//Define all the arguments for this post type.
$args = array(
'labels' => $labels,
'description' => '',
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_in_nav_menus' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => 20,
'menu_icon' => 'dashicons-images-alt2',
'can_export' => true,
'delete_with_user' => false,
'hierarchical' => true,
'has_archive' => true,
'query_var' => true,
'capability_type' => 'post',
'map_meta_cap' => true,
// 'capabilities' => array(),
'rewrite' => array(
'slug' => 'photo-gallery/%procedures%',
'with_front' => false,
'pages' => true,
'feeds' => false,
),
'supports' => array(
'title','editor','excerpt','custom-fields','revisions','thumbnail','author','page-attributes'
)
);
//Create the post type using the above two varaiables.
register_post_type( 'plugin-gallery', $args);
}
/* ====================
Taxonomy: Procedures
==================== */
add_action('init', 'plugin_register_my_taxes_procedures');
function plugin_register_my_taxes_procedures() {
$plural = __( 'Plugin Procedures' );
$singular = __( 'Plugin Procedure' );
//Used for the rewrite slug below.
$singular_slug = str_replace( ' ', '-', $singular );
$labels = array(
'name' => $plural,
'singular_name' => $singular,
'search_items' => 'Search ' . $plural,
'popular_items' => 'Popular ' . $plural,
'all_items' => 'All ' . $plural,
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => 'Edit ' . $singular,
'update_item' => 'Update ' . $singular,
'add_new_item' => 'Add New ' . $singular,
'new_item_name' => 'New ' . $singular . ' Name',
'separate_items_with_commas' => 'Separate ' . $plural . ' with commas',
'add_or_remove_items' => 'Add or remove ' . $plural,
'choose_from_most_used' => 'Choose from the most used ' . $plural,
'not_found' => 'No ' . $plural . ' found.',
'menu_name' => $plural,
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array(
'slug' => 'photo-gallery',
'with_front' => false,
),
);
register_taxonomy( strtolower( $singular_slug ), 'plugin-gallery', $args );
}
Please help. I think I have to do a rewrite rule to query taxonomy and post-name, but not sure how to start.