【问题标题】:Add some parameter to all urls in WordPress为 WordPress 中的所有 url 添加一些参数
【发布时间】:2010-11-18 00:11:15
【问题描述】:
【问题讨论】:
标签:
php
html
css
wordpress
【解决方案1】:
您应该使用 PHP cookie 来存储用户在 HTTP 请求之间的颜色偏好,但允许他们使用您正在谈论的 GET 变量覆盖它:
# Get the value of color if specified on the URL (GET) or in a cookie
# If non of those place have the color, then color will be NULL
$color = isset($_GET['color']) ? $_GET['color'] : (
isset($_COOKIE['color']) ? $_COOKIE['color'] : NULL
);
# If we know what color we have and didn't just get it from a cookie
# then set a cookie with that color as its value
if ($color != NULL && isset(! $_GET['cookie'])) {
setcookie('color', $color);
}
现在您有了$color 值,您可以按照您想要的任何方式选择样式表,例如:
<?php
if ($color != NULL) {
?> <link rel="stylesheet" href="<?php bloginfo('stylesheet_direcctory'); ?>/<?php print($color); ?>.css" type="text/css" /> <?php
}
?>
附:我的 PHP 语法有点生疏,但概念应该都在那里
【解决方案2】:
如果我的理解正确,您想根据传递的 url 参数使用不同的样式表吗?您可以在主题的头文件中执行此操作:
header.php:
//includes
<html>
...
<head>
...
<?
if($_GET['color'] == 'magic_night'){
?>
<link rel="stylesheet" href="<?php bloginfo('stylesheet_direcctory'); ?>/magic_night.css" type="text/css" />
<?
}
else
...
?>
...rest of the page...
bloginfo('stylesheet_direcctory') 应该可以将您带到您的主题目录,但我会先在测试文件中回显它,或者使用更好的参数到 bloginfo。