layers_template_locations don’t work with archive

Asked on September 8, 2015 in Development.

Hi,
I use the hook layers_template_locations to specify my template location (inside a plugin) for a custom post type. It works fine for single (ie single-mycpt.php) but not for archive (archive-mycpt.php)
Any clue ?
Thanks

matthieubrunet Layers User
Reply
  • Possible Answers to Your Question:

  • 1 Answer(s)
    Answered on September 10, 2015.

    Hi there,

    Inside your functions, you should  have a path constant:

    
    define( 'YOUR_PLUGIN_DIR' , trailingslashit( plugin_dir_path( __FILE__ ) ) );
    
    

    then in your class:

    
    public function add_template_locations( $template_locations ){
    $template_locations[] = YOUR_PLUGIN_DIR . 'templates';
    return $template_locations;
    
    }
    
    

    Then in the root of your plugin folder should be a templates folder containing your templates.

    For archives, you need to load it specifically
    https://codex.wordpress.org/Plugin_API/Filter_Reference/archive_template

    <?php
    function get_custom_post_type_template( $archive_template ) {
     global $post;
    
     if ( is_post_type_archive ( 'my_post_type' ) ) {
     $archive_template = dirname( __FILE__ ) . '/templates/post-type-template.php';
     }
     return $archive_template;
    }
    
    add_filter( 'archive_template', 'get_custom_post_type_template' ) ;
    ?>
    

    Just an update that this has been added to the plugin guide Custom Page Template article.  If this worked for you, please click the Check icon above to accept and lock the topic, thanks!

    on September 16, 2015.
    Reply