如何在 WordPress 中显示 Drupal 6.20 菜单?
是否可以在位于同一域的子目录中的 WordPress 主题模板文件中显示(通过 php)Drupal 6.20 站点的主菜单?
现在,我通过从 Drupal 站点复制静态 html 并将其添加到位于 mydomain.com/blog/ 的站点的 WordPress 模板中的 header.php 来显示菜单。但当然,当另一个菜单项添加到 Drupal 站点或 Drupal 菜单以任何方式更改时,这将不起作用。
那么,有没有一个 Drupal php 函数可以将菜单拉入 WP 文件中呢?
如果做不到这一点,有没有办法用 php 来解析 Drupal 页面的 html 菜单(是的,这会很难看)并将其显示在 WP 中?
Is is possible to display (via php) the main menu of a Drupal 6.20 site in a WordPress theme template file located in a subdirectory on the same domain?
Right now, I'm displaying the menu by copying the static html from the Drupal site and adding it to header.php in the WordPress template in the site located in mydomain.com/blog/. But of course that's not going to work when another menu item is added to the Drupal site, or the Drupal menu is changed in any way.
So is there a Drupal php function that will pull the menu into the WP file?
Failing that, is there a way with php to parse a Drupal page for the html of the menu (yes, this would be ugly) and display it in WP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
挑战的第一部分是仅输出菜单,周围的 HTML 尽可能少(或没有),因此您在解析 HTML 时要做的工作也尽可能少。
第二部分是从 Drupal 获取输出并将其实际显示在您的 WordPress 网站上。
您可以使用 $wpdb 对象,编写查询以从表中获取正确的内容,并格式化结果。这可能有效,但可能有点矫枉过正。
另一种可行的选择可能是使用 JSON 来格式化主链接的输出,使用 drupal_json 函数,然后在 Wordpress 中使用 JSON 提要。
我假设:
步骤如下:
page-node-xxxx.tpl.php
,其中 xxxx 是您的节点 ID将此代码添加到
page-node-xxxx.tpl.php
:<代码>
这将创建菜单项的 JSON feed。
访问 http://yoursite.com/admin/build 清除 Drupal 站点的主题缓存/themes 并访问 http://yoursite.com/node/xxxx 查看原始 JSON 提要。
您现在应该能够使用 jQuery 方法,例如 $.getJSON 或 < a href="http://api.jquery.com/jQuery.ajax" rel="nofollow noreferrer">$.ajax 在您的 WordPress 主题中使用和显示 JSON feed,或者可能使用 json_decode 和curl输出你的数组为 HTML。
Drupal 的
drupal_json
函数的一个好处是它已经发送了正确的 JSON 标头,因此现在您所要做的就是编写 jQuery 或 PHP 来完成您需要的操作。我假设您更像是一名 WordPress 专家,并且具有 Drupal 的应用知识,但可能对其内部工作原理不太熟悉。所以,如果它看起来太基础(或者不够基础:),我很抱歉。
The first part of the challenge is to output only the menu, with as little (or none) of the surrounding HTML as possible, so you have as little work to do in parsing the HTML as possible.
The second part is to take that output from Drupal and actually display it on your WordPress site.
You could add the Drupal database as a secondary database in WordPress using the a new instance of the $wpdb object, write the query to get the right content from the tables, and format the results. That could work, but might be overkill.
An alternative workable option may be to use JSON to format the output of the primary links, using the drupal_json function in Drupal, then consume the JSON feed in Wordpress.
I'm assuming:
The steps would be:
page-node-xxxx.tpl.php
, with xxxx being your node idAdd this code to
page-node-xxxx.tpl.php
:<?php
drupal_json(menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')));
?>
This will create a JSON feed of your menu items.
Clear the theme cache of your Drupal site by visiting http://yoursite.com/admin/build/themes and visit http://yoursite.com/node/xxxx to see the raw JSON feed.
You should now be able to use a jQuery method like $.getJSON or $.ajax in your Wordpress theme to consume and display the JSON feed, or possibly use json_decode and curl to output your array as HTML.
A good thing about Drupal's
drupal_json
function is that it already sends the correct JSON headers, so now all you have to do is write the jQuery or PHP that does what you need.I'm assumed you are more of a Wordpress specialist and have a working knowledge of Drupal but maybe not a lot of familiarity with its inner workings. So, sorry if it seemed too basic (or not basic enough :).
Drupal 主题引擎非常模块化 - 您可以对 Drupal 进行适当的 PHP 调用以仅呈现菜单,然后将该 HTML 作为 WordPress 页面的一部分发出。
The Drupal theming engine is very modular - you may be able to make an appropriate PHP call into Drupal to get just the menu rendered, then emit that HTML as a part of your WordPress page.
g_thom 的答案非常好,如果您希望创建一个非常简单的模块来输出主导航,您可以编写如下内容:
在您的 PHP 代码中,您可以编写如下内容:
我希望这有帮助!
PS:请确保更新模块的权限,以允许匿名用户访问您在模块中设置的路径 - 否则您将收到 403 Permission Denied。
g_thom's answer is very good and if you wish to create a very simple module to output the main navigation you can write something like this:
In your PHP code you can then write something like:
I hope this helps!
PS: Make sure you update the module's permissions to allow anonymous users to have access to the path you set in your module - otherwise you will get a 403 Permission Denied.