Title: Tag linking code
Last modified: December 27, 2017

---

# Tag linking code

 *  Resolved [Grego606](https://wordpress.org/support/users/grego606/)
 * (@grego606)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/tag-linking-code/)
 * Tags links on attachment page with MLA created links go to 401 page.
 * This customized code from line 263 of the functions.php file from twentytwelve-
   mla seems to be creating the link:
 *     ```
       foreach ( $terms as $term ) {
       		$output .= '<span class="terms">' . sprintf( '<a href=%1$s%2$s?my_taxonomy=%3$s&my_term=%4$s title="%5$s">%6$s</a>', $site_url, $page_path, $taxonomy, $term->slug, $title, $term->name ) . "</span> ";
       ```
   
 * How do I re-write  the code to pull in all media files that share the same att_tag?
   
   How do I add code from other classes of tags? (e.g. the standard wp tags)
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Ftag-linking-code%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [8 years, 3 months ago](https://wordpress.org/support/topic/tag-linking-code/#post-9815995)
 * Thanks for your question and for including the code fragment and a link to the
   page on your site; very helpful. Thanks as well for digging through earlier topics
   and the `twentytwelve-mla` example child theme. You probably found this recent
   topic and the earlier topics it links to:
 * [att. tags does not show in archive](https://wordpress.org/support/topic/att-tags-does-not-show-in-archive/#post-9686858)
 * I checked your site and it looks like you’ve adapted some of the solutions suggested
   in the topics and code. I couldn’t match your code to the example exactly but
   it looks like a variation on the `mla_custom_terms_list()` function in the example
   theme. The example function constructs links that go to a specific Page defined
   in your site that contains an `[mla_gallery]` shortcode to display an “archive
   listing” with items that match the selected term. The default page slug is `/
   tag-gallery/`, assigned earlier in the function. However, it looks like your 
   site does not have such a page.
 * In the header menu bar I noticed an entry titled “X”, which links to:
 * `http://bigbooknotes.org/attachment_tag/x/`
 * That page is titles “Media Archives” and looks like a functioning “archive page”
   for the Att. Tag taxonomy; is that right? If so, the easiest solution is to modify
   the code on your attachment page to link to this destination.
 *     ```
       $title = sprintf( __( 'Gallery for %1$s', 'mla-child-theme' ), $taxonomy );
       foreach ( $terms as $term ) {
           $output .= '<span class="terms">' . sprintf( '<a href="%1$s/%2$s/%3$s/" title="%4$s">%5$s</a>', $site_url, $taxonomy, $term->slug, $title, $term->name ) . "</span> ";
       }
       ```
   
 * In the above code you can see that the `href` attribute is composed from the `
   $site_url`, `$taxonomy` and `$term->slug` values. The `$title` attribute can 
   be changed to suit your needs.
 * You can use this function with any taxonomy by changing the arguments in the 
   second parameter. For example, to use the Att. Categories taxonomy you would 
   code:
 * `mla_custom_terms_list( $ID, array( 'taxonomy' => 'attachment_category' ) );`
 * If you want to display terms from multiple taxonomies you can either call the
   function multiple times or modify it to accept a list of taxonomy slugs. Here
   is one possibility for the second alternative:
 *     ```
       function mla_custom_terms_list( $ID, $attr = NULL ) {
           // Make sure $attr is an array, even if it's empty
           if ( empty( $attr ) ) {
               $attr = array();
           } elseif ( is_string( $attr ) ) {
               $attr = shortcode_parse_atts( $attr );
           }
   
           // Create the PHP variables we need
           extract( shortcode_atts( array(
               'site_url' => site_url(),
               'taxonomy' => 'attachment_tag',
           ), $attr ) );
   
           // Extract the taxonomy/taxonomies
           $taxonomies = explode( ',', $taxonomy );
   
           // Process each taxonomy, building $output as we go
           $output = '';
           foreach ( $taxonomies as $taxonomy ) {
               // Get the terms associated with the current attachment.
               $terms = get_the_terms( $ID, $taxonomy );
               if ( empty( $terms ) ) {
                   continue;
               }
   
               foreach ( $terms as $term ) {
                   $output .= '<span class="terms">' . sprintf( '<a href="%1$s/%2$s/%3$s/" title="%4$s">%5$s</a>', $site_url, $taxonomy, $term->slug, $title, $term->name ) . "</span> ";
               }// foreach term
           }
   
           // Return nothing if there are no terms
           if ( empty( $terms ) ) {
               return '';
           }
   
           // Start with a section heading, then the links
           echo "<h3>Similar Pages:</h3>\n" . $output;
       }
       ```
   
 * I haven’t tested this code so you may have to fiddle with it. With this variation
   you can display two taxonomies with something like:
 * `mla_custom_terms_list( $ID, array( 'taxonomy' => 'attachment_category,attachment_tag'));`
 * I hope the above suggestions get you started on a solution for your application.
   I will leave this topic unresolved in case you have problems or further questions
   regarding modifying the example code for your specific needs. Thanks for your
   interest in the plugin.
 *  Plugin Author [David Lingren](https://wordpress.org/support/users/dglingren/)
 * (@dglingren)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/tag-linking-code/#post-9931773)
 * It has been a month since my last post in this topic. I assume you found a solution
   that works for your application.
 * I am marking this topic resolved, but please update it if you have any problems
   or further questions regarding the above suggestions.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Tag linking code’ is closed to new replies.

 * ![](https://ps.w.org/media-library-assistant/assets/icon-256x256.png?rev=973502)
 * [Media Library Assistant](https://wordpress.org/plugins/media-library-assistant/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/media-library-assistant/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/media-library-assistant/)
 * [Active Topics](https://wordpress.org/support/plugin/media-library-assistant/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/media-library-assistant/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/media-library-assistant/reviews/)

## Tags

 * [code](https://wordpress.org/support/topic-tag/code/)
 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [links](https://wordpress.org/support/topic-tag/links/)

 * 2 replies
 * 2 participants
 * Last reply from: [David Lingren](https://wordpress.org/support/users/dglingren/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/tag-linking-code/#post-9931773)
 * Status: resolved