您可以archive_template
通过以下方案使用该钩子来处理主题的存档模板的内容,但是显然,您只能处理其中一部分主题,因为模板基本上可以包含任何旧内容。 。
该方案是将模板加载到过滤器中的字符串($tpl_str
)中archive_template
,替换您的内容,包括该字符串(使用技巧eval( '?>' . $tpl_str );
),然后返回一个空白文件,以便include
在“ wp-includes / template-loader.php”中成为空手。
以下是我在插件中使用的经过修改的代码版本,该代码版本针对使用的“经典”模板,get_template_part
并且与存档相比更关注处理单个模板,但应该可以帮助您入门。设置是插件有一个名为“模板”的子目录,该子目录包含一个空白文件(“ null.php”)和内容模板(例如“ content-single-posttype1.php”,“ content-archive-postype1.php”)以及单个案例的后备模板“ single.php”,并利用get_template_part
此目录中的自定义版本。
define( 'MYPLUGIN_FOLDER', dirname( __FILE__ ) . '/' );
define( 'MYPLUGIN_BASENAME', basename( MYPLUGIN_FOLDER ) );
add_filter( 'single_template', 'myplugin_single_template' );
add_filter( 'archive_template', 'myplugin_archive_template' );
function myplugin_single_template( $template ) {
static $using_null = array();
// Adjust with your custom post types.
$post_types = array( 'posttype1', );
if ( is_single() || is_archive() ) {
$template_basename = basename( $template );
// This check can be removed.
if ( $template == '' || substr( $template_basename, 0, 4 ) == 'sing' || substr( $template_basename, 0, 4 ) == 'arch' ) {
$post_type = get_post_type();
$slug = is_archive() ? 'archive' : 'single';
if ( in_array( $post_type, $post_types ) ) {
// Allow user to override.
if ( $single_template = myplugin_get_template( $slug, $post_type ) ) {
$template = $single_template;
} else {
// If haven't gone through all this before...
if ( empty( $using_null[$slug][$post_type] ) ) {
if ( $template && ( $content_template = myplugin_get_template( 'content-' . $slug, $post_type ) ) ) {
$tpl_str = file_get_contents( $template );
// You'll have to adjust these regexs to your own case - good luck!
if ( preg_match( '/get_template_part\s*\(\s*\'content\'\s*,\s*\'' . $slug . '\'\s*\)/', $tpl_str, $matches, PREG_OFFSET_CAPTURE )
|| preg_match( '/get_template_part\s*\(\s*\'content\'\s*,\s*get_post_format\s*\(\s*\)\s*\)/', $tpl_str, $matches, PREG_OFFSET_CAPTURE )
|| preg_match( '/get_template_part\s*\(\s*\'content\'\s*\)/', $tpl_str, $matches, PREG_OFFSET_CAPTURE )
|| preg_match( '/get_template_part\s*\(\s*\'[^\']+\'\s*,\s*\'' . $slug . '\'\s*\)/', $tpl_str, $matches, PREG_OFFSET_CAPTURE ) ) {
$using_null[$slug][$post_type] = true;
$tpl_str = substr( $tpl_str, 0, $matches[0][1] ) . 'include \'' . $content_template . '\'' . substr( $tpl_str, $matches[0][1] + strlen( $matches[0][0] ) );
// This trick includes the $tpl_str.
eval( '?>' . $tpl_str );
}
}
}
if ( empty( $using_null[$slug][$post_type] ) ) {
// Failed to parse - look for fall back template.
if ( file_exists( MYPLUGIN_FOLDER . 'templates/' . $slug . '.php' ) ) {
$template = MYPLUGIN_FOLDER . 'templates/' . $slug . '.php';
}
} else {
// Success! "null.php" is just a blank zero-byte file.
$template = MYPLUGIN_FOLDER . 'templates/null.php';
}
}
}
}
}
return $template;
}
function myplugin_archive_template( $template ) {
return myplugin_single_template( $template );
}
习惯get_template_part
:
/*
* Version of WP get_template_part() that looks in theme, then parent theme, and finally in plugin template directory (sub-directory "templates").
* Also looks initially in "myplugin" sub-directory if any in theme and parent theme directories so that plugin templates can be kept separate.
*/
function myplugin_get_template( $slug, $part = '' ) {
$template = $slug . ( $part ? '-' . $part : '' ) . '.php';
$dirs = array();
if ( is_child_theme() ) {
$child_dir = get_stylesheet_directory() . '/';
$dirs[] = $child_dir . MYPLUGIN_BASENAME . '/';
$dirs[] = $child_dir;
}
$template_dir = get_template_directory() . '/';
$dirs[] = $template_dir . MYPLUGIN_BASENAME . '/';
$dirs[] = $template_dir;
$dirs[] = MYPLUGIN_FOLDER . 'templates/';
foreach ( $dirs as $dir ) {
if ( file_exists( $dir . $template ) ) {
return $dir . $template;
}
}
return false;
}
为了完整起见,这里是使用自定义的后备“ single.php” get_template_part
:
<?php
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="clearfix">
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( $template = myplugin_get_template( 'content-single', get_post_type() ) ) include $template; else get_template_part( 'content', 'single' ); ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() ) :
comments_template();
endif;
?>
<?php endwhile; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>