• Resolved one3rdnerd

    (@one3rdnerd)


    I am using the below shortcode in the content of custom posts to display a list of associated taxonomies. This works and separates with commas and the final one has an “and” before, which is great. But I can’t seem to work out how to make these taxonomies clickable as links.

    [pods]{@talent-category}[/pods]

    I tried several other approaches to this like

    [pods][each talent-category]{@name} [/each][/pods]

    But this doesn’t separate the results with a comma.

    I also tried:

    [pods][each talent-category]{@name}, [/each][/pods]

    But this means there’s a comma at the end of the last result and no “and” before the final item.

    Is there a way to get the simpler, first example to output as links but still be separated by commas and have an and at the end?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Thread Starter one3rdnerd

    (@one3rdnerd)

    Here’s a screenshot of the two outputs.

    The first shows how I want it but the links aren’t clickable.

    The second shows them as clickable but there’s a comma at the end and no “and” before the final term.

    View post on imgur.com

    Plugin Author Jory Hogeveen

    (@keraweb)

    Hi @one3rdnerd

    I tested this locally but non of these options show a clickable link.

    For a clickable link you’d need to following:

    [pods][each category]{@name}, [/each][/pods]

    Please confirm the shortcode you used for showing clickable links.

    Cheers, Jory

    Thread Starter one3rdnerd

    (@one3rdnerd)

    Hi Jory,

    These are the two I am using (your example is the same as my second one I shared in my last comment) The only difference is you removed talent-type and changed it to category.

    [pods]{@talent-type}[/pods].

    [pods][each talent-type]{@name}, [/each][/pods]

    Please note, this relies on you having the taxonomy “talent-type” so maybe that’s why it didn’t work for you.

    My issue is, the first example is formatted perfectly, but I can’t get the terms to be clickable.

    The second one creates link, but the formatting is dreadful without commas, so I can’t find a way to use it. Adding comma’s outside of the magic tag means I end up with a comma at the very end, wheras that should be a period like in the first example.

    Please advise.

    Thanks for your time.

    Plugin Support Paul Clark

    (@pdclark)

    @one3rdnerd The following script can be zipped in a folder and installed as a plugin. It adds a shortcode [talent-category-links], which displays links to taxonomy archive pages using get_term_link(). It expects that the taxonomy terms are associated with the current post using relationship field talent-category for taxonomy talent_category:

    <?php
    /**
     * Plugin Name: Talent Category Links Shortcode
     * Description: Adds <code>[talent-category-links]</code> shortcode for displaying links to Talent Categories.
     * Version: 1
     * Author: ??δ???
     * Author URI: https://pd.cm/
     */
    
    add_action(
    	'plugins_loaded',
    	function(){
    		add_shortcode(
    			'talent-category-links',
    			function(){
    				$taxonomy = 'talent_category';
    				$field_meta_key = 'talent-category';
    
    				// Assumes Pods Admin > Settings > Override WP Metadata values is Disabled:
    				// This should return an array of term IDs, not an array of term meta.
    				$terms = get_post_meta( get_the_ID(), $field_meta_key, false );
    
    				if ( empty( $terms ) ) {
    					return '';
    				}
    
    				$term_links = [];
    				foreach( $terms as $term_id ) {
    					$term = get_term( $term_id, $taxonomy );
    
    					$term_links[] = sprintf(
    						'<a href="%s">%s</a>',
    						esc_url( get_term_link( $term ) ),
    						esc_html( $term->name )
    					);
    				}
    
    				return sprintf(
    					'<p>%s</p>',
    					implode( ', ', $term_links )
    				);
    			}
    		);
    	}
    );

    The plugin assumes Pods Admin > Settings > Override WP Metadata values is disabled.

    Thread Starter one3rdnerd

    (@one3rdnerd)

    Thanks Paul.

    I will give this a test in a moment and let you know how I get on. I will have to change the taxonomy’s underscore to a hyphen so I will do that first.

    I appreciate you.

    Plugin Support Paul Clark

    (@pdclark)

    Interesting —?I tried to have them both be talent_category, but when creating the field, it gave me an error. Hope all goes well. Thanks!

    Plugin Support Paul Clark

    (@pdclark)

    Added a small edit for the case in which no terms are assigned.
    (If empty, return nothing.)

    Thread Starter one3rdnerd

    (@one3rdnerd)

    Hi Paul,

    Firstly, thank you very much for your help.

    I just gave that a try and updated it to your updated copy.

    Unfortunately, nothing is outputting for me, but maybe that’s due to the taxonomy and field meta key being the same.

    I checked the settings, and I have Override WP Metadata values disabled.

    I changed the field key to be unique, so it’s now $field_meta_key = ‘talent-category-key’; but still nothing shows.

    I’m definitely using this on a page where terms are assigned because this is actually part of a migration from using Types Toolset (which is why I had to keep the taxonomy ID/slug as it was previously).

    Here’s a video to show you what I’m working with.

    Also, I will need to create some more copies of this plugin once working, as this is one of a few different taxonomies I need to do the same with. It would be even better if I could modify it so I could put the taxonomy slug in the shortcode rather than having it hard coded because then I could use the same plugin and shortcode for different taxonomies.

    Feel free to watch it on 1.5x speed ??

    Plugin Support Paul Clark

    (@pdclark)

    Okay, looks like it’s a core taxonomy connection, not a relationship field, so the function needs to be different. Revising…

    Thread Starter one3rdnerd

    (@one3rdnerd)

    Ah, okay, yes, it’s just a standard connection between custom post type and taxonomy rather than using relationships.

    Thank you. I will await your update.

    Plugin Support Paul Clark

    (@pdclark)

    Hi @one3rdnerd

    This version should work with the Taxonomy Connections tab. The relationship field is then not necessary.

    Usage: [term-links taxonomy="talent-category"]

    <?php
    /**
     * Plugin Name: Term Links Shortcode
     * Description: Adds <code>[term-links taxonomy="talent-category"]</code> shortcode for displaying links to taxonomy term archives.
     * Version: 2
     * Author: ??δ???
     * Author URI: https://pd.cm/
     */
    
    add_action(
    	'plugins_loaded',
    	function(){
    		add_shortcode(
    			'term-links',
    			function( $atts ){
    
    				if ( empty( $atts['taxonomy'] ) ) {
    					return '<p>Please specify a taxonomy with <code>taxonomy="talent-category"</code>.</p>';
    				}
    
    				$terms = wp_get_object_terms( get_the_ID(), $atts['taxonomy'] );
    
    				if ( empty( $terms ) ) {
    					return '';
    				}
    
    				$term_links = [];
    				foreach( $terms as $term ) {
    					$term_links[] = sprintf(
    						'<a href="%s">%s</a>',
    						esc_url( get_term_link( $term ) ),
    						esc_html( $term->name )
    					);
    				}
    
    				return sprintf(
    					'<p>%s</p>',
    					implode( ', ', $term_links )
    				);
    			}
    		);
    	}
    );
    Thread Starter one3rdnerd

    (@one3rdnerd)

    Thank you Paul.

    That one works now and works for my other taxonomy.

    Only improvement I can see.

    It removes the final comma which is great and I managed to edit it so it includes a full stop/period after the final term but is there a way I can get “and ” to show before the final term.

    I tried with the below and the period works but the and ends up at the very start of the entire output.

    
    return sprintf(
    	'<p>and %s.</p>',
    	implode( ', ', $term_links )
    

    I’m sure this shortcode plugin will be very useful for people, myself included on other sites.

    Thanks again.

    • This reply was modified 2 years, 6 months ago by one3rdnerd.
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Taxonomies Shortcode with Links’ is closed to new replies.