如何在 WordPress 中显示 Drupal 6.20 菜单?

发布于 2024-11-29 10:03:44 字数 342 浏览 0 评论 0原文

是否可以在位于同一域的子目录中的 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 技术交流群。

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

发布评论

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

评论(3

旧故 2024-12-06 10:03:44

挑战的第一部分是仅输出菜单,周围的 HTML 尽可能少(或没有),因此您在解析 HTML 时要做的工作也尽可能少。

第二部分是从 Drupal 获取输出并将其实际显示在您的 WordPress 网站上。

您可以使用 $wpdb 对象,编写查询以从表中获取正确的内容,并格式化结果。这可能有效,但可能有点矫枉过正。

另一种可行的选择可能是使用 JSON 来格式化主链接的输出,使用 drupal_json 函数,然后在 Wordpress 中使用 JSON 提要。

我假设:

  • 您拥有登录 Drupal 站点的管理员权限,您需要创建节点,并清除
  • 您想要输出主链接菜单的主题缓存,90% 以上的 Drupal 站点都使用该菜单。这可能是真的,但您的网站也可能使用自定义菜单。如果是这样,这仍然是可能的,您只需在步骤 3 中编写稍微不同的代码即可。

步骤如下:

  1. 创建一个 Drupal 节点(您可以将其称为任何名称,它只是一个占位符)
  2. 获取虚拟页面的节点 id (即节点/234)。根据节点 ID,在 Drupal 站点的主题文件夹中创建一次性页面模板。它应该被称为 page-node-xxxx.tpl.php,其中 xxxx 是您的节点 ID
  3. 将此代码添加到 page-node-xxxx.tpl.php

    <代码>

    这将创建菜单项的 JSON feed。

  4. 访问 http://yoursite.com/admin/build 清除 Drupal 站点的主题缓存/themes 并访问 http://yoursite.com/node/xxxx 查看原始 JSON 提要。

  5. 您现在应该能够使用 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:

  • you have admin access to login to the Drupal site, which you'll need to create nodes, and clear the theme cache
  • you want to output the Primary Links menu, which 90%+ of Drupal sites use. This is probably true, but it is possible your site uses custom menus. If so, this is still possible, you'd just write slightly different code in step 3.

The steps would be:

  1. Create a Drupal node (you can call it anything, it's just a placeholder)
  2. Get the node id of your dummy page (ie., node/234). From the node id, create a one-off page template in your Drupal site's themes folder. It should be called page-node-xxxx.tpl.php, with xxxx being your node id
  3. Add 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.

  4. 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.

  5. 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 :).

你爱我像她 2024-12-06 10:03:44

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.

冷︶言冷语的世界 2024-12-06 10:03:44

g_thom 的答案非常好,如果您希望创建一个非常简单的模块来输出主导航,您可以编写如下内容:

<?php

function getmenus_help($path, $arg) {
  // implementing the help hook ... well, not doing anything with it just now actually
}

function getmenus_all() {
  $page_content = '';

  $page_content = json_encode(menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')));
  // fill $page_content with the menu html

  print $page_content;

  return NULL;
}

function getmenus_menu() {
  $items = array();

  $items['getmenus'] = array(
    'title' => 'Get Menus',
    'page callback' => 'getmenus_all',
    'access arguments' => array('access getmenus'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

// permissions
function getmenus_perm() {
  return array('access getmenus');
}

在您的 PHP 代码中,您可以编写如下内容:

function primary_links() {
  $primary_links = file_get_contents(SITE_URL . '/getmenus');
  $primary_links = json_decode($primary_links);
  $primary_links = (array)$primary_links;

  $i = 0;
  $last = count($primary_links);
  $output = '';

  foreach ($primary_links as $pm) {
    $href = $pm->href;
    if (strpos($pm->href, 'http://') === FALSE) {
      if ($pm->href == '<front>') {
        $href = SITE_URL . '/';
      } else {
        $href = SITE_URL . '/' . $pm->href;
      }
    }

    $output .= '
      <li>
          <a href="'. $href .'" title="'.$pm->title.'">'.$pm->title.'</a></li>';
    $i++;
  }

  return $output;
}

我希望这有帮助!

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:

<?php

function getmenus_help($path, $arg) {
  // implementing the help hook ... well, not doing anything with it just now actually
}

function getmenus_all() {
  $page_content = '';

  $page_content = json_encode(menu_navigation_links(variable_get('menu_primary_links_source', 'primary-links')));
  // fill $page_content with the menu html

  print $page_content;

  return NULL;
}

function getmenus_menu() {
  $items = array();

  $items['getmenus'] = array(
    'title' => 'Get Menus',
    'page callback' => 'getmenus_all',
    'access arguments' => array('access getmenus'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

// permissions
function getmenus_perm() {
  return array('access getmenus');
}

In your PHP code you can then write something like:

function primary_links() {
  $primary_links = file_get_contents(SITE_URL . '/getmenus');
  $primary_links = json_decode($primary_links);
  $primary_links = (array)$primary_links;

  $i = 0;
  $last = count($primary_links);
  $output = '';

  foreach ($primary_links as $pm) {
    $href = $pm->href;
    if (strpos($pm->href, 'http://') === FALSE) {
      if ($pm->href == '<front>') {
        $href = SITE_URL . '/';
      } else {
        $href = SITE_URL . '/' . $pm->href;
      }
    }

    $output .= '
      <li>
          <a href="'. $href .'" title="'.$pm->title.'">'.$pm->title.'</a></li>';
    $i++;
  }

  return $output;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文