Creare un template di una pagina in un plugin

← ritorno a WordPress

Caricamento dei template del plugin:

// lista dei template del plugin
function norisys_components_templates() {
   $templates = array();
   $templates['page-category'] = 'Norisys Components: categoria';
   return $templates;
}
function norisys_components_templates_register( $page_templates, $theme, $post ) {
   $templates = norisys_components_templates();
   foreach( $templates as $tk => $tv ) {
      $page_templates[ $tk ] = $tv;
   }
   return $page_templates;
}
add_filter('theme_page_templates', 'norisys_components_templates_register', 10, 3 );

function norisys_components_template_select( $template ) {
   global $post;
   $pagE_temp_slug = get_page_template_slug( $post->ID );
   $templates = norisys_components_templates();
   if( isseT( $templates[$pagE_temp_slug] ) ) {
      $template = plugin_dir_path( __FILE__ ) .'templates/'. $pagE_temp_slug .'.php';
   }
   return $template;
}
add_filter('template_include', 'norisys_components_template_select', 99 );


Pagina del template ..plugin../templates/page-category.php

<?php
/**
 * Template Name: Norisys Components: categoria
 * Description: Template della pagina che rappresenta una categoria componente
 * 
 * @author Norisys srl
 */

get_header(); ?>

<div id="primary" class="site-content">
  <div id="content" role="main">

    <?php while ( have_posts() ) : the_post(); ?>
      <?php get_template_part( 'content', 'page' ); ?>
    <?php endwhile; // end of the loop. ?>

  </div><!-- #content -->
</div><!-- #primary -->

<?php get_footer();