Tag Archive for taxonomy

Create Taxonomies Programmaticallly

function updateCategory($vid, $term) {

      // Make sure the database has all the entries
      $parent = 0;
	
      // Is there an ID for this term already?
      $tid = db_result(db_query("SELECT tid FROM {term_data} WHERE vid=%d AND name='%s'",$vid,$term));
      if(!$tid) {
      // Create a new row in term_data for this term
      $tid = db_next_id('{term_data}_tid');
      db_query("INSERT INTO {term_data} VALUES(%d, 1, '%s', '', 0)", $tid, $term);
      db_query("INSERT INTO {term_hierarchy} VALUES(%d, %d)", $tid, $parent);
      }
      // The next term will be a child of this one
      $parent = $tid;
    
      // Now simply associate $tid with the product
      db_query('INSERT INTO {term_node} VALUES(%d, %d)', $product->nid, $tid);
      }

source

Render Taxonomy Terms based on Vocab ID

function render_terms($node,$vocabid,$title=null,$divclass=null) {

$terms = taxonomy_node_get_terms_by_vocabulary($node, $vocabid );
if ($terms) {

$output = '<div class="field field-item'. ' '.$divclass .'"><div class="field-label-inline-first">' .$title .' </div> ' ;
$term_links = array();
foreach ($terms as $term) {
$term_links[] = l($term->name, taxonomy_term_path($term), array('attributes' => array('rel' => 'tag', 'title' => strip_tags($term->description)))) ;
$output .= implode(", ", $term_links);
}
}
$output .='</div>';
return ($output);
}

source

Drupal print Vocabularies and Terms associated with a node

In template.php

<?php
function mythemename_taxonomy_links($node, $vid) {

//if the current node has taxonomy terms, get them
if (count($node->taxonomy)):

$tags = array();
foreach ($node->taxonomy as $term) {
if ($term->vid == $vid):
$tags[] = l($term->name, taxonomy_term_path($term));
endif;
}
if ($tags):
//get the vocabulary name and name it $name
$vocab = taxonomy_get_vocabulary($vid);
$name = $vocab->name;
$output .= t("$name") . ": " . implode(' | ', $tags);
endif;

endif;

if ($output):
return $output;
endif;

}
?>

In page.tpl.php

<?php
$nid = arg(1);
print yourthemename_print_terms($nid);
?>

source

Related nodes to the current node by taxonomy term (2)

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
$nid = (int)arg(1);
$terms = taxonomy_node_get_terms($nid);
$output = "<ul>";
foreach($terms as $term){
$sql = "SELECT DISTINCT n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE n.status = 1 AND tn.tid = $term->tid AND n.nid != $nid AND n.type <> 'image' ORDER BY n.created DESC LIMIT 6";
$result = db_query(db_rewrite_sql($sql));
if (db_num_rows($result)) {
while ($anode = db_fetch_object($result)) {
$output = node_title_list($result);
}
}
}
return $output;
}
?>

source

Related nodes to the current node by taxonomy term (1)

<?php
if (arg(0) == 'node' && is_numeric(arg(1)) && is_null(arg(2))) {
$nid = (int)arg(1);
$terms = taxonomy_node_get_terms($nid);
$output = "<ul>";
foreach($terms as $term){
$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";
$result = db_query(db_rewrite_sql($sql));
if (db_num_rows($result)) {
$output .="<li>$term->name</li><ul>";
while ($anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output.="</ul>";
}
}
$output .= "</ul>";
return $output;
}
?>

source

Drupal view to create a list of category terms

$view = new stdClass();
$view->name = 'Categories';
$view->description = 'List of categories with number of articles posted in each category.';
$view->access = array (
0 => '1',
1 => '2',
);
$view->view_args_php = '';
$view->page = TRUE;
$view->page_title = 'Browse Articles';
$view->page_header = 'Browse articles by category.';
$view->page_header_format = '1';
$view->page_footer = '';
$view->page_footer_format = '1';
$view->page_empty = '';
$view->page_empty_format = '1';
$view->page_type = 'teaser';
$view->url = 'articles';
$view->use_pager = TRUE;
$view->nodes_per_page = '100';
$view->block = TRUE;
$view->block_title = 'Categories';
$view->block_header = '';
$view->block_header_format = '1';
$view->block_footer = '';
$view->block_footer_format = '1';
$view->block_empty = '';
$view->block_empty_format = '1';
$view->block_type = 'list';
$view->nodes_per_block = '25';
$view->block_more = TRUE;
$view->block_use_page_header = FALSE;
$view->block_use_page_footer = FALSE;
$view->block_use_page_empty = FALSE;
$view->sort = array (
);
$view->argument = array (
array (
'type' => 'taxletter',
'argdefault' => '6',
'title' => '%1',
'options' => '',
'wildcard' => '',
'wildcard_substitution' => '',
),
);
$view->field = array (
array (
'tablename' => 'node',
'field' => 'title',
'label' => '',
'handler' => 'views_handler_field_nodelink',
'sortable' => '1',
'options' => 'link',
),
);
$view->filter = array (
array (
'tablename' => 'node',
'field' => 'status',
'operator' => '=',
'options' => '',
'value' => '1',
),
);
$view->exposed_filter = array (
);
$view->requires = array(node);
$views[$view->name] = $view;

source

vocabulary to options

function vocabulary_to_options($vid) {
$tree = taxonomy_get_tree($vid);
if ($tree) {
foreach ($tree as $term) {
$choice = new stdClass();
$choice->option = array($term->tid => str_repeat('-', $term->depth) . $term->name);
$options[] = $choice;
}
}
return $options;
}

source

Drupal Taxonomy Terms From CCK Taxonomy

<?php
$i = 0;
$limit = count($field_field_of_interest);
while ($i <= ($limit-1)) {
$tags[] = l($field_field_of_interest[$i]['view'], 	'taxonomy/term/'. $field_field_of_interest[$i]['tid']);
$i++;
}
if ($field_field_of_interest){
print t("Fields of Interest") . ": " . implode(' | ', $tags);
}
?>

<br />

<?php
$i = 0;
$limit = count($field_professional_area);
while ($i <= ($limit-1)) {
$tagsTwo[] = l($field_professional_area[$i]['view'], 	'taxonomy/term/'. $field_professional_area[$i]['tid']);
$i++;
}
if ($field_professional_area){
print t("Professional Area") . ": " . implode(' | ', $tagsTwo);
}
?>

source

Get Taxonomy Terms for a Vocabulary

<?php if (count($taxonomy)): ?>
<?php
$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, 1);
foreach ($terms as $term) {
$tags[] = l($term->name, taxonomy_term_path($term));
}
if ($tags){
print t("Professional Areas") . ": " . implode(' | ', $tags);
} ?>
<br />
<?php
$terms = taxonomy_node_get_terms_by_vocabulary($node->nid, 2);
foreach ($terms as $term) {
$tagsTwo[] = l($term->name, taxonomy_term_path($term));
}
if ($tagsTwo){
print t("Focus Areas") . ": " . implode(' | ', $tagsTwo);
}?>
<?php endif; ?>

source

print taxonomy terms in a vocabulary

<?php
$listlength = 99; // number of terms to show.

$vocabulary_id = 18; // VID of the vocabulary that you need to index

$result = db_query_range(db_rewrite_sql("SELECT d.tid, d.name, MAX(n.created) AS updated, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE d.vid = $vocabulary_id AND n.status = 1 GROUP BY d.tid, d.name ORDER BY name ASC"), 0, $listlength);

$items = array();
while ($category = db_fetch_object($result)) {
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid) .'<br />';
}

print theme('item_list', $items);
?>

source