我會通過創建一個頁面,然後創建一個節點模板來完成此任務。首先創建一個新節點,然後記錄模板名稱的NID。
然後,在你的模板,創建一個新的文件,並以下列方式將其命名爲:node--[node id].tpl.php
然後,在該文件中,粘貼下面的輔助函數(或者你可以把它的template.php如果你要在你的網站上的其他地方使用它):
/**
* Gets the resulting output of a view as an array of rows,
* each containing the rendered fields of the view
*/
function views_get_rendered_fields($name, $display_id = NULL) {
$args = func_get_args();
array_shift($args); // remove $name
if (count($args)) {
array_shift($args); // remove $display_id
}
$view = views_get_view($name);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
if (is_string($display_id)) {
$view->set_display($display_id);
}
else {
$view->init_display();
}
$view->pre_execute();
$view->execute();
$view->render();
//dd($view->style_plugin);
return $view->style_plugin->rendered_fields;
} else {
return array();
}
}
然後將下面的代碼添加到您的模板:
<?php
$cars = views_get_rendered_fields('view name', 'default', [...any arguments to be passed to the view]);
foreach ($cars as $car): ?>
<div>Put your mockup in here. It might be helpful to run <?php die('<pre>'.print_r($car, 1).'</pre>'); ?> to see what the $car array looks like.</div>
<?php endforeach;
?>
只要改變placeh在代碼中的任何你想要標記的東西,你應該被設置!
正如我上面提到的那樣,通過<?php die('<pre>'.print_r($car,1).'</pre>'); ?>
可以直觀地顯示數組看起來像打印的內容,總是有幫助的。
我在代碼中始終使用views_get_rendered_fields
,因爲它允許我完全自定義視圖的輸出。
作爲提醒:每次創建新模板時都要清除緩存。
祝你好運!