我以前没有做过很多关于cookies或Wordpress的工作,所以我觉得我在我的探索中有点过于乐观了:p我为我的网站创建了两个主题,夜晚和白天。我希望用户能够从侧边栏中启用php的文本小部件中选择他们想要的主题,然后根据他们的选择设置cookie,更改为适当的样式表(style.css用于night,默认值,style1.css用于day),并加载新的标题图像。
我这里有一些功能,http://somethingoriginal.net, 然而,在Chrome中,cookie似乎并没有像我所说的那样立即设置,我用来打印变量以进行测试的echo语句似乎并不一致,只需单击几下,然后最终就会更改,但标题图像根本没有改变。在Firefox中,它有时会在我的URL末尾添加反斜杠,这会将用户发送到“未找到”页面。我不知道它在IE中的作用。
我只是想知道我需要做些什么来改进功能?我不需要这样做,我只是想尝试并实现它,因为我已经制作了两幅图像:)
PHP边栏文本小部件:
getStyles();
if (isset($_COOKIE["chosenStyle"]))
echo "Your current theme is ".$_COOKIE["chosenStyle"].", enjoy!";
else if (isset($_POST[\'styles\']))
echo "Your current theme is". $_POST[\'styles\'].", enjoy!";
else
echo "Your current theme is night, enjoy!";
?>
EDIT: I have now updated my functions/header files and included the new code after reading this https://stackoverflow.com/questions/5936054/php-set-cookie-issue. The page CSS now updated automatically, I jsut need to change my text widget to reflect the choice. I am still having the header issue however
功能。php文件
function setDayHeader(){
//Set header to day header
$args = array(
\'width\' => 1000,
\'height\' => 288,
\'default-image\' => get_template_directory_uri() . \'/images/headers/SomethingOriginalSun.png\',
);
add_theme_support( \'custom-header\', $args );
}
function setNightHeader(){
$args = array(
\'width\' => 1000,
\'height\' => 288,
\'default-image\' => get_template_directory_uri() . \'/images/headers/SomethingOriginalTheMoonAndStars.png\',
);
add_theme_support( \'custom-header\', $args );
}
function getStyles() {
echo \'<form method="post" class="styles" name="styles" action="\\">
<select name="styles">
<option value="night">Night</option>
<option value="day">Day</option>
</select>
<button class="submit" name="userStyleChoiceForm" onclick="submit">Set style</button></form>\';
}
//Set a cookie for the stylesheet
if (isset($_POST["styles"])) {
$chosenStyle = ($_POST["styles"]);
setcookie("chosenStyle", $chosenStyle ,time()+31536000, "");
echo "I have set a cookie with the value ".$_COOKIE["chosenStyle"];
}
标题。php
<!-- if cookie is set, check here and then change style sheet based on cookie -->
<?php
if (!(isset($_POST["styles"]))) { //if post not set (first visit)
if (!(isset($_COOKIE["chosenStyle"])) || ($_COOKIE["chosenStyle"]) == "night") { //if cookie not set, or is night theme ?>
<link rel="stylesheet" href="<?php bloginfo(\'stylesheet_url\'); ?>" type="text/css" />
<?php
setNightHeader();
}
else { //cookie must be set to day theme, use day ?>
<link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/style1.css" type="text/css">
<?php
setDayHeader();
}
}
else { //if post is set
if (($_POST["styles"]) == "day") { //if they have chosen the day theme ?>
<link rel="stylesheet" href="<?php bloginfo(\'template_url\'); ?>/style1.css" type="text/css"> <?php
}
else if($_POST["styles"] == "night") { //if they have chsoen the night theme ?>
<link rel="stylesheet" href="<?php bloginfo(\'stylesheet_url\'); ?>" type="text/css" /> <?php
}
} ?>
我试图在标题中设置cookie。php文件,但随后出现“修改标题”错误:|
我甚至不能百分之百确定我是否引用了正确的$\\u POST变量,并且尝试了结合我找到的许多教程,因此如果有任何帮助,我将不胜感激!我意识到我正在关闭我的主站点,但无论如何它还没有完全“启动”,尽管我希望很快创建一个虚拟/测试WP站点,以便我可以在其他地方进行所有这些测试。谢谢