I want to add a custom template for a custom post ('post_type' => 'matches'
).
I am writing a plugin.I keep my template in the templates/fp-fixture-template.php
file.
Here is the function:
function fixture_template( $template_path ) {
if ( get_post_type() == 'matches' ) {
if ( is_single() ) {
// checks if the file exists in the theme first,
// otherwise serve the file from the plugin
if ( $theme_file = locate_template( array ( 'fp-fixture-template.php' ) ) ) {
$template_path = $theme_file;
} else {
$template_path = plugin_dir_path( FILE )
. 'templates/fp-fixture-template.php';
}
}
}
return $template_path;
}
Here is the filter:
add_filter( 'template_include', 'fixture_template');
Code in the fp-fixture-template.php
file:
<?php
/*Template Name: Matches Template
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Don't allow direct access
?>
<div id="primary">
<div id="content" role="main">
<?php
$mypost = array( 'post_type' => 'matches', );
$loop = new WP_Query( $mypost );
?>
<?php while ( $loop->have_posts() ) : $loop->the_post();?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php endwhile; ?>
</div>
</div>