4

I am trying to figure out how to create a url-alias friendly url to a taxonomy term page with only the tid of the term. I know drupal does this automatically in the node, but I am trying to do this in a block.

e.g. Where the term is "Texas" with tid 34, and the alias is to the taxonomy page is "/location/texas"

So, instead of creating a url to this "/taxonomy/term/34", I would like to create a url to the aliased version of that link, to /location/texas, just with the TID.

Using Drupal 7.15

Thanks!

11

If what you are asking is, "How can I create a link to a taxonomy term path alias if all I have is the TID," you can use drupal's built in l() function in any php function or template. For example, if taxonomy/term/34 exists and location/texas exists as its alias and $tid equals 34...

<?php print l('Texas Taxes', 'taxonomy/term/' . $tid); ?>

...will create <a href="/location/texas">Texas Taxes</a> because l() will by default look up and use the alias for any system path.

  • Thank you! This is exactly what I was looking for! I can't believe I couldn't find this on my own... – btotheg Aug 15 '12 at 17:49
  • @btotheg It was difficult to find this solution – chim Oct 25 '12 at 9:19
  • I'm using the l() function in a view's PHP field exactly like this, but it doesn't print the URL alias for me, just links to taxonomy/term/3. Anyone knows what to do about this? I'm sure the term has an alias. – Bálint Deáki May 6 '13 at 9:32
  • not sure what to tell you as this does work. make sure you are using a path, eg, don't start it with a leading slash (taxonomy/term/### NOT /taxonomy/term/###) and also make sure there is an alias (quick look at the url_alias table in your db or www.yourhost.com/admin/build/path/list or whatever it is in D7 :-) – Jimajamma May 6 '13 at 13:03
  • if that doesn't shed any light, make sure you aren't somehow setting the 'alias' option to TRUE in either l() directly or something down the line when it eventually calls url(), eg, check the $options in either of api.drupal.org/api/drupal/includes%21common.inc/function/l/7 or api.drupal.org/api/drupal/includes%21common.inc/function/url/7 – Jimajamma May 6 '13 at 13:07
0

If you created an alias;

/location/texas

to

/taxonomy/term/34

Then you can already create a link pointing to /location/texas. If you want to include the tid in the alias. Then just modify your alias to include the tid. Something like;

/location/texas/34

or

/location/34/texas

or

/location/34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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