WordPress.org

Ready to get started?Download WordPress

Forums

Adding page templates from a plugin (2 posts)

  1. seanuk
    Member
    Posted 1 year ago #

    I've created a plugin that adds a custom post type of 'portfolio' and I currently have to manually add archive-portfolio.php, taxonomy-portfolio.php and single-portfolio.php template files to the theme folder.

    I would like to include everything in the plugin without having to manually create/add these files to the theme folder?

    Can anyone help or advise on a decent tutorial that explains what I need

    thanks in advance :)

  2. seanuk
    Member
    Posted 1 year ago #

    ok, I found this http://codex.wordpress.org/Plugin_API/Filter_Reference/single_template and I have added 3 functions to my plugin, one for each archive_template, single_template and taxonomy_template

    is there a way to combine them into one function?

    function get_single_template($single_template) {
         global $post;
         if ($post->post_type == 'portfolio') {
              $single_template = dirname( __FILE__ ) . '/single-portfolio.php';
         }
         return $single_template;
    }
    add_filter( "single_template", "get_single_template" ) ;
    
    function get_archive_template($archive_template) {
         global $post;
         if ($post->post_type == 'portfolio') {
              $archive_template = dirname( __FILE__ ) . '/archive-portfolio.php';
         }
         return $archive_template;
    }
    add_filter( "archive_template", "get_archive_template" ) ;
    
    function get_taxonomy_template($taxonomy_template) {
         global $post;
         if ($post->post_type == 'portfolio') {
              $taxonomy_template = dirname( __FILE__ ) . '/taxonomy-portfolio.php';
         }
         return $taxonomy_template;
    }
    add_filter( "taxonomy_template", "get_taxonomy_template" ) ;

Topic Closed

This topic has been closed to new replies.

About this Topic