Adrian
Adrian

Reputation: 3062

How to add query string/params to url in WordPress?

I created a custom page template that lists posts from a "taxonomy".

When you click on a post, I want the taxonomy and term to be passed as a parameter in the url as a query string so that i can get the taxonomy on the single post page.

Any help is appreciated.

Thanks

Upvotes: 0

Views: 4950

Answers (2)

Adrian
Adrian

Reputation: 3062

I passed the value as query string to the titles within the taxonomy by using the code below:

<?php
  $theTaxonomy = get_taxonomy( get_query_var( 'taxonomy' ) ); 
  $getTaxonomy = $the_tax->labels->name;

  function to_slug($string){
      return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
  }

  $getTaxonomy;
  $getTaxonomySlug = to_slug($getTaxonomy);
  $getPermalink = esc_url(get_permalink());
  $taxonomyUrl = '?'.'type'.'='.$getTaxonomySlug;
?>

<a href="<?php echo $getPermalink.$taxonomyUrl ?>"></a>

Upvotes: 1

Duc Doan
Duc Doan

Reputation: 415

You can use add_query_arg, here is the example. Hope this help you

Upvotes: 1

Related Questions