I've got a custom post type called productpopups, and to sort them I have a custom taxonomy called productcategory. The post type has it's own special template (single-productpopup.php) and I'm trying to show each products category with a link to the category. The problem I have is the following:

The link for each productcategory is say example.com/productcategory/category but the page I want to link it to is actually example.com/category

So I made up this:

$categories = get_the_terms( $post->ID, 'productcategory' ); 
foreach( $categories as $category ) { 
    echo '<a href="http://example.com/'.$category->slug.'">'.$category->name.'</a>, '; 

This works because the pages I have set up use the same slugs as the productcategory, but its hacky and one slip (like a typo) will break it.

What I'd essential like to do is add a custom field to the taxonomy where I can specify the page url, and then be able to call that instead of the http://example.com/.$category->slug

Is this possible?

Edit for additional clarification: Forget the fact it's a url, I need a field where I can input data, and can be queried on a by product basis, so that if a product has multiple categories, the information for each can be found. For example say Product A has two categories, one and two, a table like this would be able to be formed:

| Name      | Category | Custom Value |
| Product A | C 001    | CV 001       |
| Product A | C 002    | CV 002       |

3 Answers 3


Sorry to come to the party late. Here is my solution

    //Retrieve the terms in productcategory taxonomy with posts (not empty)
    $terms = get_terms( array ( 'taxonomy' => 'productcategory', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'description', 'order' => 'ASC' ));

    //loop through each term to get its attributes
    foreach ($terms as $term) {

        //Uncomment below code to see all the available attributes.
        $name        = $term->name;

        //PHP -> Store the link in variable to reuse
        //to get the link for the the particular term; you need to have the slug and pass it into the get_term_link() function.
        //the second argument is the taxonomy name in this case productcategory.
        $cat_link    = get_term_link( $term->slug, 'productcategory' );


<a href="<?php echo $cat_link; ?>"><?php echo $name; ?></a>


  • Please take a few minutes and edit the code to explain what is going on. Currently this is a code dump.
    – user9447
    Oct 26, 2017 at 18:01
  • I hope that better explains the solution
    – omukiguy
    Oct 27, 2017 at 21:23
  • Amazing solution. Worked like a charm! Thank you.
    – Jodyshop
    Oct 5, 2022 at 20:13

If you created the taxonomy yourself, you could set your own slug for the custom taxonomy. For Example:

        'labels' => array(
            'name' => __('Categories', 'text-domain'),

        // --- set taxonomy slug ---
        'rewrite' => array(
            'slug' => '%productcategory%',  // set taxonomy slug
            'with_front' => false,          // set this to false
        // -------------------------

flush_rewrite_rules();  // <-- don't forget to add this line of code!
  • This is exactly what I've done - the thing is I have a custom page for each of these categories and I want to link to those, instead of a page where all the products are listed automatically.
    – CalvT
    Apr 25, 2016 at 15:28

The code that I finally came up with to fix the problem!

global $wpdb;
$catresult = array();
$postid = get_the_ID();
$query1c = "SELECT wp_posts.ID, wp_terms.`name` AS `Category`, wp_pods_productcategory.`pc-url` AS `Url`
FROM wp_posts
INNER JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id
INNER JOIN wp_terms ON wp_term_relationships.term_taxonomy_id = wp_terms.term_id
LEFT OUTER JOIN wp_pods_productcategory ON wp_pods_productcategory.id = wp_term_relationships.term_taxonomy_id
INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_id
WHERE wp_term_taxonomy.taxonomy = 'productcategory' AND wp_posts.ID =$postid
ORDER BY wp_terms.`name` ASC";
$categories = $wpdb->get_results( $query1c );
foreach ( $categories as $category )
array_push( $catresult, "<a href='".$category->Url."'>".$category->Category."</a>" );
echo implode(', ', $catresult);

So how does this work?

<?php This is the opening tag so the server knows what comes next is php.

global $wpdb Here we tell the server that we'll be connecting to the WordPress Database.

$catresult = array(); Here we create the array we'll use to store the data.

$postid = get_the_ID(); Here we get the post id to be able to filter the query.

$query1c = "SELECT.... Here we put the query, which selects the fields we want, filtered by wp_term_taxonomy.taxonomy = 'productcategory' and wp_posts.ID =$postid (this is where we use the $postid from before) and ordered by wp_terms.name ASC.

$categories = $wpdb->get_results( $query1c ) This runs the query, and saves the raw data to $categories.

Now we need to convert the raw data into something we can use, the raw data is currently saved as $categories but we need each result individually so we put:

foreach ( $categories as $category) Here one result gets known as $category

array_push( $catresult, "<a href='".$category->Url."'>".$category->Category."</a>" ) Here we 'push' each result individually to the array we created earlier - $catresult. Each result has been prepared with its link (<a> tag).

One last thing - as we want these categories in a list, it would look better if they where separated by commas! So:

echo implode(', ', $catresult) So here we 'implode' the array, which means we put something between each result of the array, which in this case is a comma followed by a space. This is then printed to the page by using the echo command.

Finally ?> tells the server to close this php section.

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.