• Resolved tosca30

    (@tosca30)


    I have set up my own markup in order to display my gallery, with the list of terms under each thumbnail. Currently, it looks like that : see screencopy, but I would like to add links to the corresponding archive page for each taxonomy/term.

    I have coded it that way:

    <[+itemtag+] class='gallery-item [+last_in_row+]'>
    	<[+icontag+] class='gallery-icon [+orientation+]'>
    		[+link+]
    	</[+icontag+]>
    	<[+captiontag+] class='wp-caption-text gallery-caption'>
    		[+title+]
    	</[+captiontag+]>
    	<table class='taxonomie'>
    		<tr><td class='label'>Référence : </td><td>[+custom:pic_ref+]</td></tr>
    		[+template:(<tr><td class='label'>Collection : </td><td>[+terms:post_tag+]</td></tr>|)+]
    		[+template:(<tr><td class='label'>Sujet : </td><td>[+terms:category+]</td></tr>|)+]
    		[+template:(<tr><td class='label'>Lieu : </td><td>[+terms:lieu+]</td></tr>|)+]
    		[+template:(<tr><td class='label'>Mots-clés : </td><td>[+terms:tag_perso+]</td></tr>|)+]
    		[+template:(<tr><td class='label'>Cadrage : </td><td>[+terms:cadrage+]</td></tr>|)+]
    		[+template:(<tr><td class='label'>Format : </td><td>[+terms:pic_format+]</td></tr>|)+]
    		[+template:(<tr><td class='label'>Traitement : </td><td>[+terms:traitement+]</td></tr>|)+]
    	</table>
    </[+itemtag+]>

    but I didn’t find how to add the links.

    Could you give me some hint?
    Thanks for helping.

    https://www.ads-software.com/plugins/media-library-assistant/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author David Lingren

    (@dglingren)

    Thank you for a great question, and for posting the source of your mla_markup template “Items:” section, which is an excellent example of using Content Templates to display term lists only if they are not empty. By the way, the vertical bar (‘|’) at the end of each template is not necessary; the parentheses around the template value are enough to suppress display of empty [+terms: ... +] values.

    Moving on to your question, I suggest adding some code to the “mla_gallery_item_values” filter to get what you want. I am adding an example to the /media-library-assistant/examples/mla-hooks-example.php.txt sample plugin for a solution. Here is the code I am adding:

    /*
     * Our third example changes taxonomy terms into links to term-specific archive pages.
     */
    if ( 'term gallery' == self::$shortcode_attributes['my_filter'] ) {
    	/*
    	 * Use the "my_href" parameter to link to a static page,
    	 * passing the taxanomy and term as query arguments.
    	 */
    	if ( isset( self::$shortcode_attributes['my_href'] ) ) {
    		$my_href = self::$shortcode_attributes['my_href'];
    	} else {
    		$my_href = '';
    	}
    
    	/*
    	 * Collect non-empty term lists, convert to slugs,
    	 * make into links, replace $item_values
    	 */
    	foreach ($item_values as $key => $value ) {
    		if ( ( 'terms:' == substr( $key, 0, 6 ) ) && ( ! empty( $value ) ) ) {
    			$taxonomy = substr( $key, 6 );
    			$terms = array_map( 'trim', explode( ',', $value ) );
    			foreach( $terms as $term_name ) {
    				$term_object = get_term_by( 'name', $term_name, $taxonomy );
    
    				if ( empty( $my_href ) ) {
    					$term_links[] = sprintf( '<a href=/%1$s/%2$s>%3$s,</a>', $taxonomy, $term_object->slug, $term_name );
    				} else {
    					$term_links[] = sprintf( '<a href=%1$s?my_taxonomy=%2$s&my_term=%3$s>%4$s,</a>', $my_href, $taxonomy, $term_object->slug, $term_name );
    				}
    			}
    
    			$item_values[ $key ] = implode( ' ', $term_links );
    		}
    	}
    
    	return $item_values;
    }

    To activate this code, add my_filter="term gallery" to the shortcode. I have also added my_href, so you can change the destination page. My shortcode looks like:

    [mla_gallery mla_markup=france post_parent=all my_filter="term gallery" my_href="/tag-gallery/"]

    Note that I have substituted a static WordPress page for “the corresponding archive page“. WordPress taxonomy archive pages are designed to display posts and pages; they do not work for attachments. I have found that a static page with an [mla_gallery] shortcode is the easiest alternative. The /tag-gallery/ page would include:

    [mla_gallery tax_query="array(array( 'taxonomy' => '{+request:my_taxonomy+}', 'field' => 'slug', 'terms' => '{+request:my_term+}'))"]

    Of course, you can also define an archive.php template and use do_shortcode() to generate the gallery. To do that, just omit the my_href parameter and the code will generate a link to the archive page.

    I am marking this topic resolved, but please update it if you have any problems or further questions. Thanks for inspiring an example that should be of interest to many other MLA users.

    Thread Starter tosca30

    (@tosca30)

    Thanks a lot for having provided this complete example.
    I have made a few modifications:
    – initialize the $term_links array in the first foreach loop, as each new taxonomy-term of an item was added to the previously computed (1 line for the 1st, line 1 and 2 for the 2nd, etc)
    – add a get_site_url() to the links, as my site is not at the root
    – suppress the comma after each term.

    So, here is the final code I’m going with:

    /*
     * Our third example changes taxonomy terms into links to term-specific archive pages.
     */
    if ( 'term gallery' == self::$shortcode_attributes['my_filter'] ) {
    	/*
    	 * Use the "my_href" parameter to link to a static page,
    	 * passing the taxanomy and term as query arguments.
    	 */
    	if ( isset( self::$shortcode_attributes['my_href'] ) ) {
    		$my_href = self::$shortcode_attributes['my_href'];
    	} else {
    		$my_href = '';
    	}
    
    	/*
    	 * Collect non-empty term lists, convert to slugs,
    	 * make into links, replace $item_values
    	 */
    	foreach ($item_values as $key => $value ) {
    		if ( ( 'terms:' == substr( $key, 0, 6 ) ) && ( ! empty( $value ) ) ) {
    			$taxonomy = substr( $key, 6 );
    			$terms = array_map( 'trim', explode( ',', $value ) );
    			$term_links = array();
    			foreach( $terms as $term_name ) {
    				$term_object = get_term_by( 'name', $term_name, $taxonomy );
    
    				if ( empty( $my_href ) ) {
    					$term_links[] = sprintf( '<a href=%1$s/%2$s/%3$s>%4$s</a>', get_site_url(), $taxonomy, $term_object->slug, $term_name );
    				} else {
    					$term_links[] = sprintf( '<a href=%1$s/%2$s?my_taxonomy=%3$s&my_term=%4$s>%5$s</a>', get_site_url(),$my_href, $taxonomy, $term_object->slug, $term_name );
    				}
    			}
    
    			$item_values[ $key ] = implode( ' ', $term_links );
    		}
    	}
    
    	return $item_values;
    }

    I want to use the same archive page whatever taxonomy is being called, so I’ll probably go for a regular archive.php for the whole site (it’ll also be called from a customized search form) and won’t use the my_href parameter.
    In fact, that is precisely the reason for my other question: Using tax_query parameter in do_shortcode

    Plugin Author David Lingren

    (@dglingren)

    Thank you for cleaning up my code and posting the complete source of your final version. You’re right – initializing $term_links is required for multiple taxonomies. I like your idea to add get_site_url() to the href= attribute and I have added both of your updates to my example version.

    Thread Starter tosca30

    (@tosca30)

    I’m coming for a little problem: when terms contain a quote (Sur l’eau, for example) the link doesn’t work.
    Something might be missing, so PHP wouldn’t get lost in the process.

    Plugin Author David Lingren

    (@dglingren)

    I regret the issue you’re having with quote characters in your term names. This appears to be caused by some encoding the WordPress sanitize_term_field function is performing.

    I believe you can correct the issue by adding one line of code to your filter, between the $taxonomy = and $terms = lines. Change your code to look like:

    $taxonomy = substr( $key, 6 );
    $value = str_replace( '’', "'", $value );
    $terms = array_map( 'trim', explode( ',', $value ) );

    You must also re-encode the term name by adding esc_html to your sprint statements, like this:

    if ( empty( $my_href ) ) {
    	$term_links[] = sprintf( '<a href=%1$s/%2$s/%3$s>%4$s,</a>', get_site_url(), $taxonomy, $term_object->slug, esc_html( $term_name ) );
    } else {
    	$term_links[] = sprintf( '<a href=%1$s/%2$s?my_taxonomy=%3$s&my_term=%4$s>%5$s</a>', get_site_url(),$my_href, $taxonomy, $term_object->slug, esc_html( $term_name ) );
    }

    That should correct the problem. If you have any problems with it, let me know.

    Thread Starter tosca30

    (@tosca30)

    I’m afraid I don’t see any difference after having modified the code.
    If you navigate from this page https://www.marie-noelle-augendre.com/cevennes/galerie/ you will see that all the sujet that have either Sur l’eau or Au bord de l’eau term have an incomplete link (taxonomy is there, but no term afterwards).

    Alternatively, if you choose the very same sujet from the searchform in the sidebar, the links work fine and you are redirected to the right page that contain the chosen pictures.

    [Moderator Note: Please ensure that you are embedding links correctly in your posts.]

    Thread Starter tosca30

    (@tosca30)

    Oups! I might have forgotten to close the link tag, and can’t see any way to correct my previous message.
    Here is the link to that page.

    Plugin Author David Lingren

    (@dglingren)

    Thank you for posting the update and for the link to the example on your site, which is very helpful. There are two flaws in my earlier response (which I regret):

    1. My source code example got distorted by the HTML processing of the support forum. The HTML escape sequence I intended to illustrate was replaced by a “right-single-quote” character.
    2. Your site works somewhat differently than my site; it uses a different escape sequence.

    Here is an improved version of the first change I suggested to your code:

    $taxonomy = substr( $key, 6 );
    $value = str_replace( '& #8217;', "'", $value );
    $value = str_replace( '& rsquo;', "'", $value );
    $terms = array_map( 'trim', explode( ',', $value ) );

    I have inserted a space after the ampersand in the first str_replace argument. You must remove this space in the actual code. I have added a line for each of the two escape sequences you might find in the term values; one of these should work for you (probably the second).

    Give that a try and let me know how it goes. Thanks for your patience.

    Thread Starter tosca30

    (@tosca30)

    Problem solved with this last modification.
    Thanks a lot for your helpfulness.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Field-level markup parameters with links’ is closed to new replies.