Embedding Views in PHP snippets with Views 2

21st May
2008

Just a quickie - embedding views in PHP snippets etc. Sometimes blocks or panels don't quite cut the mustard and we need to directly insert a view via some PHP.

I started off here - the method I've used before to achieve this. There's another howto on Innovating Tomorrow. However, views_build_view, and indeed theme_view, are both no longer part of Views...

After a bit of searching I found mention of a new function; its also mentioned in the Views 2 documentation (work in progress).

So, the resulting PHP is wonderfully simple:

print views_embed_view($view_name, $display_id = 'default');

I was tearing my beard out, so I hope this helps someone.

Tags:

Comments

Awesome

Thanks for the awesome tip. The documentation didn't make any sense for embedding. Cheers!

embed views

I can't seem to get this to work to save my life ...

here is the function i'm calling ...

any hints would be greatly appreciates:

<?php print views_embed_view($recent_stories-block_1, $display_id = 'default'); ?>

NVM ...

This seemed to work better for my situation:

<?php $viewName = 'MYVIEWNAME'; print views_embed_view($viewName); ?>

Thanks for at least getting me pointed in the right direction.