skip to Main Content

How can I get custom taxonomy page URL in PHP on WordPress?
I have created custom taxonomy name "developer" for posts core on WordPress, this is the URL https://check1.site/developer/Fingersoft/.
Each post has one or 2 developers tag assigned.
Now I want to call this URL in PHP, but I am not sure what code should I use to echo this URL in posts

I tried get_post_type_archive_link() but it returns current post URL not the developer URL

2

Answers


  1. You can only get the URL(s) of each custom taxonomy term(s), but not the URL of your custom taxonomy itself. For that, you will simply use the function get_term_link() WordPress function like in the example below:

    $taxonomy = 'developer'; // Your custom taxonomy slug
    
    // Get your custom taxonomy term(s) from an existing post ID
    $terms = wp_get_post_terms( $post->ID, $taxonomy );
    $html = ''; // initializing variable for output
    
    // Loop through each custom taxonomy WP_Term in the current post 
    foreach ( $terms as $term ){
       $term_link =  get_term_link( $term, $taxonomy ); // Get the term link
       
       $html .= '<li><a href="'.$term_link.'">'.$term->name.'</a></li>'; // formatting for output
    }
    
    echo '<ul>' . $output . '</ul></pre>'; // Displaying formatted output
    

    This will display a list of the "Developer" linked term(s) for the current post.

    Login or Signup to reply.
  2. The function you are looking for is get_term_link. It takes either a term object, ID or slug and a taxonomy name and returns a URL to the term landing page.

    $terms = get_terms('developer');
    echo '<ul>';
    foreach ($terms as $term) {
        echo '<li><a href="'.get_term_link($term->slug, 'developer').'">'.$term->name.'</a></li>';
    }
    echo '</ul>';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search