您可以使用archive_template
通过使用下面的方案,钩子来处理主题归档模板的内容,但很明显,由于模板基本上可以包含任何旧内容,因此您只能处理一小部分主题。
方案是将模板加载到字符串中($tpl_str
) 在archive_template
过滤、替换内容、包括字符串(使用技巧eval( \'?>\' . $tpl_str );
), 然后返回一个空白文件,以便include
在“wp-includes/template-loader.php”中,变成了一个no-op。
下面是我在插件中使用的代码的黑客版本,其目标是使用get_template_part
与归档相比,它更关心的是处理单个模板,但应该可以帮助您开始。设置是,插件有一个名为“templates”的子目录,其中包含一个空白文件(“null.php”)和内容模板(例如“content-single-posttype1.php”、“content-archive-posttype1.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(); ?>