Is there a way to suppress the multiple taxonomy terms that appear in the page title?

For example: taxonomy/term/10+16+17+18+19+20+21+22+23+24

The title appears as "Title 1, Title 2, Title 3, Title 4, Title 5, Title 6, Title 7, Title 8, Title 9, Title 10"

I'd like to either define a single custom title for the group or just use term 10's title.

Comments

nancydru’s picture

johnhanley’s picture

I'm already using URL Alias to set a path for the taxonomy terms.

Drupal insists on displaying ALL the term names associated with the URL Alias. I need a way to suppress this behavior and use only one term for the title.

nancydru’s picture

You'll probably need a small module that uses hook_nodeapi and/or hook_link_alter to change the display.

Edit: Try drupal_set_title

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

jastraat’s picture

I use a function in my template.php file to create customized titles out of multiple fields for custom content types (i.e - first name + last name for a person node). You might be able to apply something like this to your situation. I haven't tested the following code, but it's just an idea:

function _phptemplate_variables($hook, $vars = array()) {

  switch ($hook) {
     case 'page':
        if (arg(0) == 'taxonomy') {
	     drupal_set_title('Custom Group Name');
	}
     break;
  }
  return $vars;
}

--------------

http://fraggles.artsci.wustl.edu (Drupal user documentation and development blog)

johnhanley’s picture

jastraat, thanks for your suggestion. It definitely clued me in on where to go and what to do.

However instead of drupal_set_title() perhaps a better way is to change the title variable value. For example:

function _phptemplate_variables($hook, $vars = array()) {
  if ($hook == 'page') {
    if (arg(0) == 'taxonomy') {
      $term = taxonomy_get_term(arg(2));
      $vars['title'] = $term->name;
    }
  }
  return $vars;
}

Note that if you're using an existing theme like Garland (versus a custom one) you'll want to most like modified the existing _phptemplate_variables function.

Many thanks.

jastraat’s picture

-------

http://fraggles.artsci.wustl.edu (Drupal user documentation and development blog)

jefftrnr’s picture

BacteriaMan - your solution is brilliant!
Something I've added to your if() adds on the vocab name too... if that helps others needing the same

using $term from your code....

      $vocab = taxonomy_get_vocabulary($term->vid);
      $vars['title'] = $vocab->name . ": " . $term->name;

Finding this post was a big help, thanks so much!

johnhanley’s picture

jefftrnr,

I'm happy you found this thread helpful.

Speaking of taxonomy, I remember another problem with the same project was displaying taxonomy breadcrumbs properly. I found the Taxonomy Breadcrumb module extremely useful.

Take care,
John

Compel’s picture

I am not sure where this snippet described above would go, so I just experimented and placed it as a standalone in the template.php file. I also did a version with the function named fusion_core_phptemplate_variables....

/**
 * Modify Taxonomy Title
  */
   
function _phptemplate_variables($hook, $vars = array()) {
  if ($hook == 'page') {
    if (arg(0) == 'taxonomy') {
      $vocab = taxonomy_get_vocabulary($term->vid);
      $term = taxonomy_get_term(arg(2));
      $vars['title'] = $vocab->name . ": " . $term->name;
    }
  }
  return $vars;
}

Of course just cutting and pasting as noobs tend to do gets the predictible result of a broken site :-) I'd appreciate help knowing where in template.php the function goes. Also, if it would be better placed within another function (for example between the first IF and the RETURN statement of this function placed in a page preprocessing function.

I'm putting it in Fusion Core so it will work for Acquia marina and Acquia Prosper.

modul’s picture

Just happened to stumble across this thread. It's a bit old, but maybe someone can help me out. I am trying to use an url like http://mysite.com/?q=taxonomy/term/123+567. Individually, the terms do exist, i.e. both terms are attached to some nodes. But when I write them combined with "+", as the Handbook says I'm allowed to do, and as this thread also deals with, I get a page title with both terms (the words, not the id's) combined with "+", but for the rest my page is blank, i.e. no nodes show up. Any idea what I could be doing wrong??

nhck’s picture

...that the changed this to:
http://mysite.com/?q=taxonomy/term/123 567

Anyway.. I think it is too bad that this is the only solution to this problem.

nhck’s picture

... i made the mistake to digg this up (confused the year), here is my solution for drupal 6:

insert into template.php

/**
* The title for multiple taxonomy terms can be quiet long (as described here: {@link http://drupal.org/node/158243})
*
* This fixes the current page title to only display the topmost/lightest term
*
* @todo make sure you are using the correct function name (replace THEMENAME by your theme name)
* @todo If there are multiple vocabularies or equal level terms one of them is randomly picked :-)
* @todo Terms must be separted by a space
* @param $variables Array of variables available to theme
*
*/
function THEMENAME_preprocess_page(&$variables)
{
    if (arg(0) != 'taxonomy')  return; //not taxonomy? -> exit

    $terms = explode(" ",arg(2));

    if(count($terms) <= 1) return; //only one term? -> exit

    foreach($terms as $term) {

        $children = _taxonomy_term_children($term); //get all children of the term

        if(empty($children)) {
             continue; //no children? Go for another round
        }
        else {
            foreach($children as $child) {
                $findings = array_keys($terms,$child); //look if any of the children are part of the called terms
                if(!empty($findings)) {
                    foreach($findings as $found) unset($terms[$found]); //yes? get rid of the child found
                }
            }
        }
    }
    $finally = taxonomy_get_term(array_pop($terms));
    $variables['head_title'] =  str_replace($variables['title'],$finally->name,$variables['head_title']) ;
   $variables['title'] = $finally->name; //remove this line if you do not want the page title chagned
}
julianna’s picture

Hi, Niels

I tried this code in my template.php file (remembering to change "THEMENAME" to the theme name), but it is still showing all of the terms rather than just one. Does this still work in 6.16?

Thanks

julianna’s picture

Nevermind. It seems to work now that the website and my browser cache has cleared.

Thanks!

nhck’s picture

Thanks for the heads-up :-)

Compel’s picture

Just a reminder that the template.php page for Fusion templates is in the Fusion Core template. If you can't find a template.php in a subtheme, check in the parent theme's folder. Sounds like common sense but these things do escape noobs like myself.

Compel’s picture

There's no reason I can figure, but this breaks the styles for FF and Chrome using Fusion themes.

It sometimes breaks IE8 as well. Causes the right sidebar to disappear and the content to be under the left sidebar. I placed it in the page preprocessing function of the Fusion Core template.php (everything from the beginning if statement through the end where it says to comment out if you do not want the title removed).

Also changed the $variables variable to $vars to match the naming in the current template.php.

Using 6.19 drupal and Fusion 3.0/Acquia_Marina with Skinr 1.5.