【发布时间】:2010-12-07 23:24:56
【问题描述】:
一天后更新:现在正在设置 Cookie。我尝试通过下面引用的代码中的编辑来显示我需要进行的更改。还是有问题。现在它没有应用非默认样式表!浏览器可以找到它们,但切换器找不到....
(原始问题:) 我在 WP3 博客(站点为 here)中使用 Rob Ballou 的 v.2 Styleswitcher (robballou.com/switcher/v2/tutorial.php)。我已经更改了样式表 url,以便 WP 知道在哪里可以找到它们:
发件人:$ss->addStyle("default", "style.css", "", "", true);
到
$my_url = get_bloginfo('template_url');
$ss->addStyle("default", $my_url . '/style.css', "", "", true);
从文档来源判断,正在找到样式表。所以这不是问题。但是,cookie 只是没有设置。如果我在网页上运行完全相同的脚本(例如 molvray.com/testing/ss-example.php),它可以工作,并且当我检查 cookie 时,cwStyle 已设置。但是一旦它在 WP 中,就没有运气了。
我猜问题出在这行:$ss->cookieDomain = ".". $_SERVER['HTTP_HOST']; in switcher.php。我看到一篇建议改用 HTTP_REFERRER 的帖子,但这无济于事。我在下面包含了 switcher.php。我还没有触及 Styleswitcher.php 本身。
谁能告诉我我做错了什么?我要疯了!
请尽量保持低调,这样我就有滚雪球般的机会追随。我在编程方面很没用。
新:大部分 switcher.php 都包含在 header.php 中,而不是作为单独的文件。
在 header.php 中:
<?php
require_once("Styleswitcher.php");
$ss = new Styleswitcher('/switcher/v2/');
if(!isset($reqPath)){ $reqPath = ""; }
require_once($reqPath ."Styleswitcher.php");
$my_url = get_bloginfo('template_url');
$ss = new Styleswitcher();
$ss->addStyle("default", $my_url . '/style.css', "", "", true);
$ss->addStyle("lowgraphics", $my_url . '/handheld.css');
$ss->addStyle("highcontrast", $my_url . '/highcontrast.css');
$ss->createSet("style");
$ss->addStyleToSet("style", "default", true);
$ss->addStyleToSet("style", "lowgraphics");
$ss->addStyleToSet("style", "highcontrast");
$ss->cookieDomain = ".". $_SERVER['HTTP_HOST'];
$ss->cookieName = "cwStyle";
$ss->start();
$ss->printStyles();
?>
实际的 switcher.php 就是:
<?php
if(!isset($reqPath)){ $reqPath = $my_url; }
require_once("Styleswitcher.php");
$ss = new Styleswitcher('/switcher/v2/');
$ss->cookieDomain = ".". $_SERVER['HTTP_HOST'];
$ss->cookieName = "cwStyle";
$ss->start();
?>
wordpress 的 header.php 中使用的表单具有“<_ bloginfo>/switcher.php” 而不仅仅是“switcher.php”
<form action="<?php bloginfo('template_url'); ?>/switcher.php" method="post">
<_input type="hidden" name="referer" id="referer" value="">
<input type="hidden" name="inputStyle2" id="inputStyle2" value="style" />
<li><input type="submit" name="style" id="default" value="Default" <?php $ss->printSetInputChecked("style", "default"); ?>/></li>
<li><input type="submit" name="style" id="lowgraphics" value="Mobile" <?php $ss->printSetInputChecked("style", "lowgraphics"); ?>/></li>
<li><input type="submit" name="style" id="highcontrast" value="High Contrast" <?php $ss->printSetInputChecked("style", "highcontrast"); ?>/> </li>
</form>
我认为,关键是在表单元素中将 php bloginfo('template_url') 路径添加到 switcher.php。现在开始弄清楚为什么它除了默认样式之外什么都没有。
【问题讨论】:
-
你在哪里运行代码的主要块?在模板文件中?如果有,是哪个文件?在插件内部?
-
主要代码在Styleswitcher.php中,下载后没有变化。 switcher.php 调用它,并包含您可能需要更改的设置。两者都在模板文件的博客目录中。我在 switcher.php 中更改的是“myurl”行和样式文件的路径。
-
Switcher.php 在模板文件 header.php 中被调用
-
TheDeadMedic:我已经设法让它设置 cookie(虽然我不明白为什么)并且我已经更新了我原来的问题。不过,它会应用的唯一样式是默认样式,所以我还没有。 :(