wordpress functions.php包含两个单独自定义标题的不同脚本和样式

时间:2015-10-19 04:23:50

标签: wordpress custom-headers

我正在开发一个wordpress web应用程序,并希望有两个不同的头文件,我可以使用,具体取决于我所在的网站部分。例如,如果未登录则显示自定义标头#1,如果已登录则显示自定义标头#2。我想使用functions.php文件将每个头文件上的不同脚本和样式文件排入队列。问题是我需要在每个头文件上调用wp_head(),这意味着他们将加载相同的脚本和文件。任何建议将不胜感激。

2 个答案:

答案 0 :(得分:2)

WordPress'get_header()函数可用于通过传入参数来包含不同的标头模板。例如:

// index.php
get_header(); // Will include header.php.

// some-template.php
get_header('other'); // Will include header-other.php.

当标头依赖于模板文件时,这是执行此操作的首选方法。例如,如果您需要为登录用户显示不同的标题,以下是一个快速解决方案:

if (is_user_logged_in()) :
    get_header(); // Includes header.php.
else :
    get_header('guest'); // Includes header-guest.php.
endif;

要将不同的脚本和样式排列到不同的模板和页面,您只需挂钩wp_enqueue_scripts挂钩并确定使用WP条件注册和入队的内容:

add_action('wp_enqueue_scripts', function () {
    wp_register_script('script-for-home', $urltoscript, ...);
    wp_register_script('script-for-page', $urltoscript, ...);
    wp_register_script('script-for-template', $urltoscript, ...);

    if (is_home()) {
        wp_enqueue_script('script-for-home');
    } elseif (is_page()) {
        wp_enqueue_script('script-for-page');
    } elseif (is_page_template('template.php')) {
        wp_enqueue_script('script-for-template');
    }
});

wp-admin中的条件队列有点棘手,但请查看wp_get_current_screen以查看当前的管理页面。

答案 1 :(得分:-2)

function wpd_enqueue_scripts() {
    if( is_user_logged_in() ){
        wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/logged-in.js', array(), '1.0.0', true );
 wp_enqueue_style( 'twentythirteen-bootstrap.min', get_template_directory_uri() . '/css/bootstrap.min.css', array(), 'v3.1.0' );
    } else {
        wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/not-logged-in.js', array(), '1.0.0', true );
 wp_enqueue_style( 'twentythirteen-bootstrap.min', get_template_directory_uri() . '/css/bootstrap.min1.css', array(), 'v3.1.0' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpd_enqueue_scripts' );