I know how to print a block in a node but how do I add a "menu block" in a node? I'm hoping this is something easy that I am just overlooking so any help is appreciated.

<?php
$block
= module_invoke('block', 'block', 'view', 1);
print
$block['content'];
?>

I tried modifying the code that allows you to print blocks in nodes with no luck.

Thanks!

Comments

Buddharide’s picture

Here's how to do it in Drupal 6. Simply change the "1" to the number of the block.

<?php
//下在的代码绝对有大用
$block
= module_invoke('menu_block', 'block', 'view', '1');
print
$block['content'];
?>

Namaste.
Adam Hegi
primalmedia.com

Buddharide’s picture

That method doesn't display the title for me.

Buddharide’s picture

Have you tried printing $block['title']?

Buddharide’s picture

Even more straightforward, without using a separate module:

<?php
$block
= module_invoke('menu', 'block', 'view', 'menu-your-menu-name');
print
$block['content'];
?>
Buddharide’s picture

In Drupal 7 hook_block has changed to hook_block_op, so in order to make the above(comment #1 and #4) work you need to make some simple changes, namely changing ('block', 'op') to 'block_op'.

Example:

$block = module_invoke('menu', 'block_view', 'menu-my-menu-name');
print render($block['content']);

This will work similarly to what worked in Drupal 6, as demonstrated in comment #4 above.

Buddharide’s picture

@james: <?php $block = module_invoke('menu', 'block_view', 'menu-main-menu'); ?>Doesnt do it for me, where do i find the menu name????? Why isnt that the block id?

Buddharide’s picture

Doesn't do it either:

<?php $block = module_invoke('menu_block', 'block_view', 1); ?>

Just prints Array.

Buddharide’s picture

Off course: print render($block['content']);.

For others:

<?php $block = module_invoke('menu_block', 'block_view', 1); ?>
<?php print render($block['content']); ?>
Buddharide’s picture

No solution to this yet?

Trying the options above give an array... or prints the wrong menu.

menu-block module doesn't provide an ID number in the block admin screen.

Buddharide’s picture

it does in the url/path

Buddharide’s picture

@ice5nake: You can get the title with
<?php print $block['subject']; ?>

Buddharide’s picture

@lasac This does also work in Drupal 7:

<?php
block_render('menu_block', 'NUMBER_OF_MENU_BLOCK');
?>

Retrieve the number from Admin/Structure/Blocks and hovering over the "configure" link. The number is in this link after ...manage/menu_block/

Buddharide’s picture

Why so many different answers, none of which work?! It's a simple and straightforward question yet completely undocumented.

block_render is an undefined function, no idea why that would be working for you.

Really like this module but what is the actual, official way of embedding a block in PHP? None of the solutions here work for me.

Buddharide’s picture

I can confirm that #9 works

Buddharide’s picture
Issue summary:View changes

I have tested in D7, here the codes work for me :)

To embed menu block:

<?php
 $block
= module_invoke('menu_block', 'block_view', 3);
 print
render($block['content']);
?>

To embed original "Main menu" (NOT "menu-main-menu"):

<?php
 $block
= module_invoke('menu', 'block_view', 'main-menu');
 print
render($block['content']);
?>

To embed other menu beside main menu as #6 @jamesrutherford pointed out.

<?php
 $block
= module_invoke('menu', 'block', 'view', 'menu-your-menu-name');
 print
$block['content'];
?>

e.g, 'menu-product'

Buddharide’s picture

Any ideas if/or how does the module_invoke work with the i18n menu blocks?

Thanks,


来自 https://www.drupal.org/node/505712