确定页面内容类型

发布于 2024-09-13 10:57:10 字数 617 浏览 2 评论 0原文

我正在 Drupal 6 中工作。

我需要在用户位于博客页面上时添加特定块。听起来很简单,但它却让我发疯。

当用户查看博客概述或单个博客条目时,需要显示该块。

我最初认为我可以按页面名称过滤它,因此它仅在页面 = /blog/ 时出现。不幸的是,这仅适用于博客概述页面;各个博客条目页面都有自己的 URL(默认为 /node/,但将更改为所有者想要的任何内容)。

再用谷歌搜索一下,我发现了 $node->type=='blog' ,它应该表明我在博客条目页面上,但似乎不起作用。

在 admin/build/block/configure 页面中,我将页面可见性设置为 PHP 模式,PHP 代码如下:

<?php
return ($node->type == 'blog');
?>

但这似乎不起作用,即使我在模板中 print_r($node) ,它确实显示类型==博客。

我还在上面添加了 strpos($_SERVER['REQUEST_URI','blog') ,但当然,由于第一个条件不起作用,添加第二个条件也无济于事。

感觉应该有一个明显的答案,但我就是找不到。谁能帮我一下。谢谢。

I'm working in Drupal 6.

I have a requirement to add a particular block when the user is on a blog page. Sounds simple enough, but it's been driving me mad.

The block needs to be shown when the user is viewing a blog overview or an individual blog entry.

I initially thought I could filter it by page name so it only appears when page = /blog/. Unfortunately, that only applies to the blog overview page; the individual blog entry pages have their own URLs (default is /node/ but will be changed to whatever the owner wants).

A bit more googling, and I found out about $node->type=='blog' which should pick up the fact that I'm on a blog entry pages, but doesn't seem to work.

In the admin/build/block/configure page I have page visibility set to PHP mode, and PHP code as follows:

<?php
return ($node->type == 'blog');
?>

but that doesn't seem to work, even though if I print_r($node) in the template, it does show type==blog.

I also added strpos($_SERVER['REQUEST_URI','blog') to the above, but of course since the first condition isn't working, adding the second won't help.

It feels like there should be an obvious answer, but I just can't find it. Can anyone help me out. Thanks.

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

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

发布评论

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

评论(1

羁拥 2024-09-20 10:57:10

上面代码的问题是,当您运行该块的代码时,它不会有可用的 $node 变量。您需要执行类似的操作才能将其添加到博客节点。

<?php
    // This code checks the internal url, which for nodes always will be node/[nid].
    // Last condition: don't display the block on node edit forms etc.
    if (arg(0) == 'node' && is_numeric(arg(1)) && empty(arg(2))) {
      $node = node_load(arg(1));
      return $node->type == 'blog';
    }
?>

Your problem with the above code, is that when you run the code for the block, it wont have the $node variable available. You need to do something like this to add it to blog nodes.

<?php
    // This code checks the internal url, which for nodes always will be node/[nid].
    // Last condition: don't display the block on node edit forms etc.
    if (arg(0) == 'node' && is_numeric(arg(1)) && empty(arg(2))) {
      $node = node_load(arg(1));
      return $node->type == 'blog';
    }
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文