我想为自定义帖子添加自定义模板(\'post_type\' => \'matches\'
).
我正在写一个插件。我将模板保存在templates/fp-fixture-template.php
文件
功能如下:
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;
}
以下是过滤器:add_filter( \'template_include\', \'fixture_template\');
中的代码fp-fixture-template.php
文件:<?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>