Drupal 7:查看节点时隐藏带有空字段的标签

发布于 2024-11-29 18:44:47 字数 67 浏览 1 评论 0原文

在查看特定内容类型的实际节点时,如何隐藏具有空字段的标签?

我真的很感谢任何人的帮助,感谢您的宝贵时间。

How do I hide labels that have empty fields when viewing the actual node of a certain content type?

I'd really appreciate anyone's help, thanks for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

感受沵的脚步 2024-12-06 18:44:47

实现此目的的另一种方法是使用适用于该内容类型的所有节点的自定义模板文件。

确保 node.tpl.php< /a> 首先存在于您的 sites/all/themes/[mytheme] 目录中。在调用其他自定义模板之前,该模板必须存在。

复制您的node.tpl.php并将其命名为node--[contenttype].tpl.php(不带括号)。

如果您启用了 Devel 模块,则可以抛出 dpm($content); 进入文件以找出您要隐藏的字段的名称。或者您可以查看内容类型本身。

获得字段名称后,您现在可以在 print render($content); 语句之前插入此代码:

if (empty($content['my_field'])) {
  unset($content['my_field']);
}

清除缓存,并且仅当存储了值时才会显示您的字段。

Another way you could achieve this is by using a custom template file that would apply to all nodes of that content type.

Make sure that node.tpl.php exists in your sites/all/themes/[mytheme] directory first. This template must exist before other custom templates can be called.

Make a copy of your node.tpl.php and name it node--[contenttype].tpl.php (without the brackets).

If you have the Devel module enabled, you can throw a dpm($content); into the file to find out the name of the field you are trying to hide. Or you could look at the content type itself.

Once you have the name of the field, you can now insert this code before the print render($content); statement:

if (empty($content['my_field'])) {
  unset($content['my_field']);
}

Clear the cache, and your field will only appear if there is a value stored.

半世蒼涼 2024-12-06 18:44:47

默认情况下,空字段的标签是隐藏的,也许该字段中仍然存在“不间断空格”或其他剩余内容?
您必须检查出现问题的现有节点与未触及特定字段的新节点之间的差异。

设置隐藏在 nl/admin/struct/types/manage/selected_content_type/display 中的不需要的标签显示

By default, the labels of empty fields are hidden, maybe there's still a 'non breaking space' or some other leftover in the field?
You have to check the difference between an existing node where the problem occurs and a new node where you don't touch the particular field.

Set unwanted labels display hidden in nl/admin/structure/types/manage/selected_content_type/display

风启觞 2024-12-06 18:44:47

我想纠正第一个答案。在node.tpl.php中,我们应该检查#markup而不是字段数组:

if (empty($content['field_vac_req'][0]['#markup'])) {
  unset($content['field_vac_req']);
}

而不是

if (empty($content['my_field'])) ...

I would like to correct first answer. In node.tpl.php we should check #markup instead of field array:

if (empty($content['field_vac_req'][0]['#markup'])) {
  unset($content['field_vac_req']);
}

instead of

if (empty($content['my_field'])) ...
红颜悴 2024-12-06 18:44:47

如果内容类型有很多循环字段,这对我有用:

    foreach($content AS $key => $values) {
       if (!empty($content[$key][0]['#markup'])) {
          print render($content[$key]);
          }
    }

If the content type has a lot of fields looping this worked for me:

    foreach($content AS $key => $values) {
       if (!empty($content[$key][0]['#markup'])) {
          print render($content[$key]);
          }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文