作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在编写一个 WP 插件,需要覆盖模板。
我的过滤器钩子(Hook)看起来像这样 - 它执行:
add_filter('template_include', 'mcd_set_template',10);
最佳答案
您可以使用如上所示的 template_redirect,但这确实需要退出,并且它确实践踏了 WordPress 通常会执行的所有其他操作来查找当前模板。您可能希望让这种情况发生,然后将逻辑应用于当前模板。
使用上面的一些东西......
add_action('template_include', 'mcd_set_template');
function mcd_set_template() {
return locate_template('templatename.php');
}
add_action('template_redirect', 'mcd_set_template');
function mcd_set_template() {
/**
* Order of templates in this array reflect their hierarchy.
* You'll want to have fallbacks like index.php in case yours is not found.
*/
$templates = array('templatename.php', 'othertemplate.php', 'index.php');
/**
* The first param will be prefixed to '_template' to create a filter
* The second will be passed to locate_template and loaded.
*/
include( get_query_template('mcd', $templates) );
exit;
}
add_filter('category_template', 'filter_category_template');
function filter_category_template($template){
/* Get current category */
$category = get_queried_object();
/* Create hierarchical list of desired templates */
$templates = array (
'category.php',
'custom-category-template.php',
'category-{$category->slug}.php',
'category-{$category->term_id}.php',
'index.php'
);
return locate_template($templates);
}
关于WordPress template_include - 如何正确 Hook ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8320750/
我目前正在编写一个 WP 插件,需要覆盖模板。 我的过滤器钩子(Hook)看起来像这样 - 它执行: add_filter('template_include', 'mcd_set_template'
以下代码用于覆盖产品详细信息页面模板,自 WooCommerce 上次更新以来它一直在工作。任何人都可以帮助我解决这个问题,在此先感谢。 add_filter('template_includ
我是一名优秀的程序员,十分优秀!