[wp-hackers] Template Hierarchy In A Plugin

Alex Andrews awgandrews at gmail.com
Sun Aug 7 17:01:27 UTC 2011


Thanks very much. That's sort of what I meant, however, what I really wanted is the ability to 'virtually' extend the template directory. So I could just run a filter or function then the directory ribcage/templates (say) would be a directory that is also automatically searched for the template of my custom post type. Might this be a good thing to suggest in a patch if it doesn't exist?

I guess what I could do is duplicate the intelligent loader from the template file (i.e. the thing which tells it if we have a custom post type then look here and here), but this seems a rather long winded way and I thought there might be something more elegant. I guess I could hook in at the point of the code you have kindly provided and do this somewhat automatically. 

Thanks lot.

Alex 

-- 
Alex Andrews
Sent with Sparrow (http://www.sparrowmailapp.com/?sig)

On Sunday, 7 August 2011 at 16:40, Mike Little wrote:

> On Fri, Aug 5, 2011 at 23:25, Alex Andrews <awgandrews at gmail.com (mailto:awgandrews at gmail.com)> wrote:
> 
> >  Dear all,
> > 
> > What is the best way to take advantage of the way in which WordPress
> > automatically guesses, and correctly loads the right template for a custom
> > post type, but within a plugin rather than a theme?
> > 
> > So in a plugin I load a series of custom post types, and tell WordPress
> > where the template files for them are, which it then loads in an automated
> > manner. So album-single.php loads as we would expect if it were under the
> > template directory and so on?
> > 
> > I attempted to Google this, but obviously quite difficult to get through
> > the thickets of results to it out!
> > 
> > Thanks for the help,
> > 
> > Alex
> If I understand you correctly, you want to load a template file to display a
> custom post type from your plugin directory, whereas WordPress would
> normally look for the appropriate template file in a theme.
> 
> You should look at the 'template_include' filter. Something like this code
> (not tested) might work.
> 
> add_filter( 'template_include', 'template_include' );
> function my_template_include( $template ) {
> if ( 'album' === get_query_var( 'post_type' ) ) {
> if ( is_single() ) {
>  return dirname( __FILE__ ) . '/single-album.php';
> } else { // loop
>  return dirname( __FILE__ ) . '/archive-album.php';
> }
> }
>  return $template;
> } // end my_template_include
> 
> 
> Mike
> -- 
> Mike Little
> http://zed1.com/
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com (mailto:wp-hackers at lists.automattic.com)
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list