5

我正在尝试为我的用户提供更改页面上使用的背景图像的功能。

背景图像列表将是一个很小的数字,不会真正改变。

我想我可以添加一些分类术语……每种背景类型一个……然后在查看页面时将一个类应用于 body 标签。

这听起来可行吗?如果可行,我将如何去做?

谢谢

山姆

4

1 回答 1

1

编辑:在澄清我对问题的误解后修改答案

如果要按(节点)页面定义背景图像,那么您通过分类词汇表的方法听起来是正确的方法。要使这些术语可用于 CSS,最简单的方法是将它们作为类输出/使用在 node.tpl.php 文件中,您可以在其中直接访问$node变量。但在这种情况下,它们有点隐藏在生成的标记的中间,这使得正确使用它们有点困难。

为了将它们添加到$body_classespage.tpl.php 中的变量中,您必须操作zen_preprocess_page()函数来添加它们,或者(更好的方法)preprocess_page()使用 zen 函数将它们添加到您自己的模块/主题函数中举个例子:

function yourModuleOrTheme_preprocess_page(&$vars) {
  // Add classes for body element based on node taxonomy
  // Is this a node page?
  if ('node' == arg(0) && is_numeric(arg(1))) {
    // Yes, extract wanted taxonomy term(s) and add as additional class(es)
    $node = node_load(arg(1));
    $background_vid = yourFuntionToGetTheBackgroundVocabularyId(); // Could be hardcoded, but better to define as variable
    $terms = $node['taxonomy'][$background_vid];
    foreach ($terms as $tid => $term) {
      // NOTE: The following assumes that the term names can be used directly as classes.
      // You might want to safeguard this against e.g. spaces or other invalid characters first.
      // Check the zen_id_safe() function for an example (or just use that, if zen is always available)
      $vars['body_classes'] .= ' ' . $term;
    }
  }
}

注意:未经测试的代码,可能包含拼写错误和其他疏忽。


编辑前的原始答案- 基于对 OP 意图的误解,留下她以防其他人也误解它:)
基本想法听起来是可行的,但我建议一个小的变化:

由于您希望每个用户都可以调整设置,因此您必须跳过一些环节才能让用户使用分类术语“标记”自己。我认为仅启用(核心,但可选)配置文件模块并在那里配置“背景”字段(类型为“列表选择”)会容易得多。该字段将显示在用户页面上(或该页面上的单独选项卡,如果您给它一个类别),并且稍后可以很容易地从代码中获得用户选择,例如为页面模板派生一个类:

global $user;
// NOTE: The following call would be the explicit way,
// but usually the profile fields get added to the $user object
// automatically on user_load(), so you might not need to call it at all,
// extracting the values directly from the $user object instead
$profile = profile_load_profile($user);
$background = $user->profile_background
于 2010-05-20T18:24:19.000 回答