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.
-
Hey Sohail,I come back stackexchange ,again see you with hard questions boy :D . hard question need bounty :D– YuseferiCommented Jun 18, 2016 at 8:59
-
Welcome back bro! :D I'll give you 500 bounty since you just asked :D– SohailCommented Jun 20, 2016 at 9:22
-
1Tnx Dear, your questions often are particular that need exclusive answers ;)– YuseferiCommented Jun 20, 2016 at 9:35
Add a comment
|
1 Answer
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']);
}
-
Thanks a lot Yusof, I'll take a look into your solution and let you know– SohailCommented Jun 20, 2016 at 9:33
-
-
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 thenid
of the node to redirect it to– SohailCommented 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']
.– YuseferiCommented Jun 29, 2016 at 7:07 -
it still doesn't work with
$items[0]['nid']
, and unfortunatelydsm($items)
is empty, I wonder if we can access nodes associated with terms like this– SohailCommented Jul 2, 2016 at 2:25