您甚至没有提到的第一个问题是:您正在将短代码添加到主题中。Shortcodes are pure plugin territory, 因为他们正在改变帖子内容,而且必须在主题切换后生存下来。
一旦您修复了这个问题并将短代码移动到名为inferno_shortcodes
, 剩下的很简单:
在插件中请求主题支持:
$default_templates = array(
\'stacked\' => \'stacked.php\',
\'one_half\' => \'one_half.php\',
\'one_half_last\' => \'one_half_last.php\',
);
$theme_templates = (array) get_theme_support( \'inferno_shortcodes\' );
$templates = array();
foreach ( $default_templates as $shortcode => $file )
{
if ( isset ( $theme_templates[ $shortcode ] ) )
$templates[ $shortcode ] = locate_template( $theme_templates[ $shortcode ] );
else
$templates[ $shortcode ] = plugin_dir_path( __FILE__ ) . "templates/$file";
}
现在,任何主题都可以为所有或某些短代码提供模板…
add_theme_support(
\'inferno_shortcodes\',
array (
\'stacked\' => \'inferno-shortcodes/stacked.php\',
\'one_half\' => \'inferno-shortcodes/one_half.php\',
\'one_half_last\' => \'inferno-shortcodes/one_half_last.php\',
)
);
…在插件中,您只需包含用于呈现输出的模板。