0

How can I get the ID of a taxonomy term with only a path alias, using PHP?

I have a vocabulary with some taxonomy terms with a path alias like [vocabulary]/[term] (e.g. car/model) and I have repeated model terms. (So I think that using titles is not an option.)

I created a view with a path (car/%); if I want to make another view with an extra argument and car/%/% as path, I can't get the content unless I use the taxonomy ID like car/123/1 instead of car/model/cat.

Is there a way to make this?

4
  • I would suggest to use a parameter "?parameter=1" instead another argument in this case
    – DarkteK
    Commented Jun 12, 2016 at 4:13
  • can you explain me this a little more please? what do you mean? Commented Jun 12, 2016 at 4:29
  • You can keep using your taxonomy id and also a parameter, example: car/123?model=Toyota, is it a good option for you?
    – DarkteK
    Commented Jun 13, 2016 at 5:33
  • maybe that can work, but, can i make a diferent views if a set the view path with something like car/model?cat=123 (will display products of model and cat 123) and other view with only path car/model to display subcategories? this because i want to use path alias instead of tid's because url's looks ugly lol. Also, you know if there is a way to get the tid with only a partial part of the path alias, e.g. get the tid of full path car/toyota-1 or car/toyota-0 with only the arg toyota-1 or toyota-0? Commented Jun 13, 2016 at 14:33

1 Answer 1

1

I solved the problem with this:

e.g. i have the path alias car/ilx-0 and i can get the tid easily, but what if i want to pass only the ilx-0 to another path view, like: products/ilx-0, the path alias doesn't match anymore so it's not easy to get the tid, to achieve that i used the following code on products/% view:

    <?php
    $pa=drupal_get_normal_path('car/'. arg(1, request_uri())); /* get the ilx-0 from products/ilx-0 and transform to taxonomy/term/tid */
    list($t1, $t2, $t3) = explode("/", $pa); /* Explode taxonomy/term/tid */
    /* $t1 = taxonomy */
    /* $t2 = term */
    return $t3; /* tid, return this value to get tid and pass to context filter*/
    ?>
1
  • Well done, it was kind of hard what you were asking sorry
    – DarkteK
    Commented Jun 15, 2016 at 22:42

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.