1

So I'd like to redirect taxonomy term page or show the full content of the node on taxonomy page if there's only one node associated with that term.

3
  • Hey Sohail,I come back stackexchange ,again see you with hard questions boy :D . hard question need bounty :D
    – Yuseferi
    Commented Jun 18, 2016 at 8:59
  • Welcome back bro! :D I'll give you 500 bounty since you just asked :D
    – Sohail
    Commented Jun 20, 2016 at 9:22
  • 1
    Tnx Dear, your questions often are particular that need exclusive answers ;)
    – Yuseferi
    Commented Jun 20, 2016 at 9:35

1 Answer 1

1

My solution is handle it in hook_preprocess_page(maybe there is another or better solutions but this is good too)

put this in template.php

first solution(maybe not worked)

function YOURTEMPLATE_preprocess_page(&$variables) {
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    $tid = arg(2);
    $items = taxonomy_select_nodes($tid;
    if(count($item)==1){
         drupal_goto('node/'.$items[0]->nid);
     }
    }
}

Alternative solution

function YOURTEMPLATE_preprocess_page(&$variables) {
  if (arg(0) == 'taxonomy' && arg(1) == 'term' && is_numeric(arg(2))) {
    $tid = arg(2);
    $items = taxonomy_select_nodes($tid);
    if(count($items)==1){

         //another solution is set a variable in preprocess and in page.tpl.php or your taxonomy tempalte page redirect it 
         $variables['soheil_redirect_tax'] = array('path' => url('node/'.$items['nid']));

    }
  }

if first solution (drupal_goto) not worked in page.tpl.php or your custom template for terms page handle redirect(I mean in page.tpl.php file or taxonomy--term.tpl.php file) with

 if(isset($soheil_redirect_tax)){
    drupal_goto($soheil_redirect_tax['path']);
 }
7
  • Thanks a lot Yusof, I'll take a look into your solution and let you know
    – Sohail
    Commented Jun 20, 2016 at 9:33
  • @Sohail You're welcome,Ok ;)
    – Yuseferi
    Commented Jun 20, 2016 at 9:36
  • Hey Yusef, it almost worked with the second approach, it does redirect when there's only 1 taxonomy term on the page, but on my localhost it redirects to ~sohail/helger/~sohail/helger/node/, as you can see it repeats the base url and does not load the nid of the node to redirect it to
    – Sohail
    Commented Jun 29, 2016 at 3:01
  • @Sohail try to dsm($items) to see what you have there,maybe its a array then you have to replace it with $items[0]['nid'].
    – Yuseferi
    Commented Jun 29, 2016 at 7:07
  • it still doesn't work with $items[0]['nid'] , and unfortunately dsm($items) is empty, I wonder if we can access nodes associated with terms like this
    – Sohail
    Commented Jul 2, 2016 at 2:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.