为未登录用户显示不同的主题

时间:2016-12-28 作者:pixeline

基于answers like this one, 我制作了一个小插件来显示一个主题,显示一个“即将到来”的主题,而我们的编辑团队可以填写最终的主题。

它昨天起作用了,但今天,即使我登录了,我也只看到了“等待”主题。我注销并重新登录,但仍然看到了“等待”主题,好像is_user_logged_in() 函数返回false。这些挂钩检查用户身份验证是否为时过早?

<?php

add_filter(\'template\', \'pxln_change_theme\');
add_filter(\'stylesheet\', \'pxln_change_theme\');

function pxln_change_theme($theme) {    
    if ( ! is_user_logged_in() ) {
        $theme = \'waiting\';
    }
    return $theme;
}

2 个回复
SO网友:cjbj

Filters, 与操作不同,不在特定时刻运行,而是在调用它们所附加的函数时运行。模板筛选器从调用get_template 和样式表过滤器get_stylesheet.

通常,调用这些函数以使用wp_enqueue-scripts, 一action that takes place after 已设置当前用户。但是,这些函数也可以从附加到after_setup_theme, 在设置当前用户之前激发。

因此,您必须检查主题的使用get_templateget_stylesheet (或使用它们的函数,可以在上面链接的“used by”下找到)。然后检查功能是否连接到过早的挂钩。

SO网友:yivi

尝试勾住所有这些:

(但我想你必须从插件开始,因为从主题开始functions.php 可能太晚了)。

E、 g.:

/*
Plugin Name: test
Description: switchtest
Version: 0.1.0
*/

add_filter( \'template\', \'yourthing_switch_theme\' );
add_filter( \'option_template\', \'yourthing_switch_theme\' );
add_filter( \'option_stylesheet\', \'yourthing_switch_theme\' );
add_filter( \'pre_option_stylesheet\', \'yourthing_switch_theme\' );

function yourthing_switch_theme( $theme )
{
   if ( is_user_logged_in() ) {
        return $theme;
   }
   else {
      return \'waiting\';
   }
}

相关推荐

WP_Query filters

这个问题可能看起来很愚蠢,但我真的没有找到有用的链接。我正在尝试做一个简单的查询,在这里我选择了最近的10篇文章,我希望能够在循环中只显示这10篇文章中的1篇我的代码看起来像 // Query latest 10 records $the_query = new WP_Query( array( \'post_type\' => \'post\', \"order\" => \"DESC\", \'posts_per_page