2013-02-12 41 views
1

我想打印taxonomy_vocabulary_11數組(這是一個Drupal 7站點)的所有結果。 如果我使用<?php print render($content['taxonomy_vocabulary_11'][0]['#title']); ?>,我只會得到一個結果。使用PHP以可編譯的方式打印所有數組結果?

從來就usuccessfully試圖

<?php foreach ($content->taxonomy_vocabulary_11 as $key => $value): $terms = $value['#title']; ?> 
<?php print $terms; ?> 
<?php endforeach?> 

我得到這個錯誤:Notice: Trying to get property of non-object

現在,這是我開始使用輸出devel的模塊(dpm($node);

(object) array(
    'vid' => '5178', 
    'uid' => '1', 
    'title' => 'PROYECTO', 
    'log' => '', 
    'status' => '1', 
    'comment' => '2', 
    'promote' => '0', 
    'sticky' => '0', 
    'nid' => '155', 
    'type' => 'jornadas', 
    'language' => 'und', 
    'created' => '1095048000', 
    'changed' => '1360589684', 
    'tnid' => '0', 
    'translate' => '0', 
    'revision_timestamp' => '1360589684', 
    'revision_uid' => '1', 
    'taxonomy_vocabulary_4' => array(), 
    'taxonomy_vocabulary_6' => array(
    'und' => array(
     array(
     'tid' => '33', 
     'taxonomy_term' => (object) array(
      'tid' => '33', 
      'vid' => '6', 
      'name' => 'Jornadas Gratuitas', 
      'description' => '', 
      'format' => NULL, 
      'weight' => '0', 
      'vocabulary_machine_name' => 'vocabulary_6', 
     ), 
    ), 
    ), 
), 
    'taxonomy_vocabulary_7' => array(
    'und' => array(
     array(
     'tid' => '40', 
     'taxonomy_term' => (object) array(
      'tid' => '40', 
      'vid' => '7', 
      'name' => 'Para PH', 
      'description' => '', 
      'format' => NULL, 
      'weight' => '-10', 
      'vocabulary_machine_name' => 'vocabulary_7', 
     ), 
    ), 
    ), 
), 
    'taxonomy_vocabulary_11' => array(
    'und' => array(
     array(
     'tid' => '262', 
     'taxonomy_term' => (object) array(
      'tid' => '262', 
      'vid' => '11', 
      'name' => 'colegios', 
      'description' => '', 
      'format' => NULL, 
      'weight' => '0', 
      'vocabulary_machine_name' => 'vocabulary_11', 
     ), 
    ), 
     array(
     'tid' => '543', 
     'taxonomy_term' => (object) array(
      'tid' => '543', 
      'vid' => '11', 
      'name' => 'derecho', 
      'description' => '', 
      'format' => NULL, 
      'weight' => '0', 
      'vocabulary_machine_name' => 'vocabulary_11', 
     ), 
    ), 
    ), 
), 
    'body' => array(
    'und' => array(
     array(
     'value' => " 

從來就也嘗試將<?php print render($content['taxonomy_vocabulary_11']); ?>作爲任何其他領域的分類法,但它不會打印任何內容。如果我去做print $content['taxonomy_vocabulary_11'];它只是打印字數組。

我的方法有什麼問題?

回答

1

嘗試:

foreach ($content['taxonomy_vocabulary_11'] as $tv11) { 
    print render($tv11['#title']); 
} 

我不使用Drupal,但如果我的理解是怎麼$內容[ 'taxonomy_vocabulary_11']數組結構這應該工作。

+0

感謝您的回覆奧茲。但是,我收到此錯誤消息:「致命錯誤:只能通過引用傳遞變量」。 – Rosamunda 2013-02-12 15:09:21

0

萬一有人需要它:

<?php 
$vid = 11; //vocabulary id 
$nid = $node->nid; //it looks for the current loaded node 
$query = "SELECT tid, name 
FROM (
SELECT td.tid AS tid, name 
FROM taxonomy_term_data AS td 
JOIN taxonomy_index AS tn 
    ON td.tid = tn.tid 
JOIN node AS n 
    ON n.nid = tn.nid 
WHERE td.vid = ". $vid ." 
    AND n.status = 1 
    AND n.nid = ".$nid." 
GROUP BY td.tid 
) AS t 
ORDER BY name ASC"; 
$result = db_query($query); 
foreach($result as $term) { 
     echo l($term->name, "taxonomy/term/$term->tid") . ', '; 
} 
?> 
+0

請注意!此代碼有安全問題:http://drupal.stackexchange.com/questions/61009/is-there-an-easier-way-to-print-taxonomies-in-the-tpl-i-get-this-working-碼 – Rosamunda 2013-02-12 16:58:50