Drupal 仅根据位置显示某些内容
我需要能够仅根据用户在世界上的位置显示某些内容/节点。例如,如果用户来自美国,他们应该看到与来自欧洲的用户不同的结果。
理想情况下,我能够使用应该显示的区域来标记每个节点,然后当显示内容时,它会被这些标签过滤。
我还将使用各种内容“滚动条”来显示内容,并且我希望这些滚动条也能根据位置具有不同的内容。
我怎样才能最好地实现这一目标?
I need to be able to only display certain content/nodes based on where a user is located in the world. For example, if the user is from the US, they should see different results than someone from Europe.
Ideally I would be able to tag each node with the region(s) that it should be displayed in and then when the content is displayed, it is filtered by these tags.
I'm also going to be using various content 'scrollers' to display content and I would like these to have different content based on location as well.
How would I best achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用视图 - 和很酷的模块,例如:
基本上您想首先获取用户的位置,这将存储在会话中。
一旦你有了它,你就可以随时参考它。
Use Views - and cool modules like:
Basically you want to first get the location of the user, this will be stored in the session.
once you have that you can reference it any time.
我知道这是一个旧线程,但在 Drupal 中不一定有简单的解决方案。我是这样做的。该解决方案与原始问题并不完全匹配,但这解释了一种可以通过扩展 PHP 代码来将用户位置与分配给每个节点的分类术语进行比较来扩展以涵盖该场景的方法。
首先,安装IP2Country。这允许您通过进行地理位置查找来将 IP 地址与某人所在的国家/地区相匹配。
其次,使用这样的 PHP 代码来确定访问者的 IP 地址是否与某个国家/地区匹配。如果访问者位于美国或加拿大,则此示例返回 true,否则返回 false。
您可以在不同的地方使用此代码,但您需要启用 PHP 过滤器模块才能使其工作。
在我的解决方案中,我以两种方式使用此代码:
首先,我为节点模板(也可以是分类术语模板)创建了一个面板变体,以返回 HTTP 响应而不是面板窗格。我使用此代码作为选择规则,因此如果用户不在允许查看该特定节点类型的国家/地区,它将返回 404。然后,我创建了面板变体,该变体为根据其位置符合资格的任何人返回节点页面。
其次,我为节点类型(在本例中为产品)创建了一个面板变体,并使用相同的 PHP 代码为那些因其位置而不允许查看它们的用户隐藏页面上的某些元素。在本例中,我隐藏了产品显示页面上的“价格”和“添加到购物车”窗格。允许国家/地区之外的访问者只能看到产品信息,而看不到页面上的电子商务元素。
作为第三个选项,您还可以使用它来使用相同的代码显示/隐藏 Drupal 块。
最后,我创建了一个自定义主题功能,这样我就可以根据一个人的位置显示/隐藏各个菜单项。在本例中,我想隐藏“商店”菜单项,除非您位于允许的国家/地区。我在这里更详细地写了如何做到这一点: https:// FiveMinutelessons.com/learn-drupal-sitebuilding/show-or-hide-menu-item-drupal-7-based-users-location
I know this is an old thread, but it's one that doesn't necessarily have an easy solution in Drupal. Here's how I did it. This solution doesn't match the original question exactly, but this explains a method that could be extended to cover that scenario by extending the PHP code to compare the user's location to the taxonomy term(s) assigned to each node.
First, install IP2Country. This allows you to match an IP address to someone's country by doing a geolocation lookup.
Second, use PHP code like this to determine if the visitor's IP address matches a certain country. This example returns true if the visitor is in the US or Canada, and false if they are not.
You can use this code in various places, but you will need to enable the PHP filter module for it to work.
In my solution, I used this code in two ways:
First, I created a Panels Variant for a Node Template (could be a Taxonomy Term template as well) to return a HTTP response rather than a Panel Pane. I used this code as a selection rule, so it would return a 404 if the user was not in a country that was allowed to see that specific node type. I then created Panels variant that returned the node page for anyone who qualified based on their location.
Second, I created a Panels variant for a node type (Product, in this case) and used the same PHP code to hide certain elements on the page for those users who weren't allowed to see them because of their location. In this case, I hid the Price and Add to Cart panes on a product display page. Visitors outside the allowed countries only saw the product information, and not the ecommerce elements on the page.
As a third option, you could also use it to show/hide Drupal blocks using the same code.
Finally, I created a custom theme function so I could show/hide individual menu items depending on a person's location. In this case, I wanted to hide the Shop menu item unless you were in the permitted countries. I wrote about how to do that in more detail here: https://fiveminutelessons.com/learn-drupal-sitebuilding/show-or-hide-menu-item-drupal-7-based-users-location