按分类术语与当前节点相关的节点

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了按分类术语与当前节点相关的节点相关的知识,希望对你有一定的参考价值。

Use the code below to build a custom block that shows the related nodes to the current node by taxonomy term. For example: If the current node in display has a taxonomy term "fish", a bunch of links that link to nodes that have the "fish" term is to be shown in a block on the page.
  1. <?php
  2. if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
  3. $nid = (int)arg(1);
  4. $terms = taxonomy_node_get_terms($nid);
  5. $output = "<ul>";
  6. foreach($terms as $term){
  7. $sql = "SELECT n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid = $term->tid AND n.nid != $nid LIMIT 5";
  8. $result = db_query(db_rewrite_sql($sql));
  9. if (db_num_rows($result)) {
  10. $output .="<li>$term->name</li><ul>";
  11. while ($anode = db_fetch_object($result)) {
  12. $output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
  13. }
  14. $output.="</ul>";
  15. }
  16. }
  17. $output .= "</ul>";
  18. return $output;
  19. }
  20. ?>

以上是关于按分类术语与当前节点相关的节点的主要内容,如果未能解决你的问题,请参考以下文章

Drupal 8 如何制作仅显示与当前节点相关的分类术语的视图

PHP 通过分类术语到当前节点的相关节点(1)

PHP 通过分类术语到当前节点的相关节点(2)

按分类术语对分组节点进行排序

如何创建按分类术语对节点进行分组的视图?

Drupal Views:获取与当前节点具有相同分类的节点