在文件名而不是模板上排队

时间:2018-09-20 07:48:56

标签: php wordpress

我有sidebar.php和sidebar-closing.php。

假设我有dashboard.php:

<?php
/**
* Template name: Virtuallio Dashboard
*/
?>

<?php include( get_stylesheet_directory() . '/virtuallio-template-sidebar.php'); ?>

/*Content goes here*/

<?php include( get_stylesheet_directory() . '/virtuallio-template-sidebar-closing.php'); ?>

我想保持它的动态,所以我不想在每次创建新页面时都将“ if(is_page('new-page.php')”添加到functions.php中。

我想发生的是在sidebar.php上加入脚本和样式,因为它遍及整个页面(应用程序)。我不需要App外部的某些样式。

所以它是这样的:if ($filename == "sidebar.php") { /* enqueue here */}

有任何解决方法吗?

1 个答案:

答案 0 :(得分:0)

使用get_sidebar()函数代替include,则可以

function my_enqueued_styles($name) {
 // Check $name here if you want
 wp_enqueue_style($handler, $path);
}
add_action('get_sidebar', 'my_enqueued_styles');

另一种解决方法是

function custom_sidebar() {
 require_once('...');
 do_action('custom_action');
}

function my_enqueued_styles() {
 wp_enqueue_style($handler, $path);
}
add_action('custom_action', 'my_enqueued_styles');

然后将边栏称为custom_sidebar();