• Hi,

    This is a very simple question, but I just can’t do it.
    Please tell me how to do it.

    I have added a custom post type.
    Next, I created a custom taxonomy associated with the above custom post type. The only content is plain text. I’m using it like a standard wordpress tag.

    I want to display the contents of this custom taxonomy inside a query loop.
    If I use the Pods Field Value block, I can display it, but I get “,” and “and” at the end of each.
    I would like to display them separated by div or span.

    Please help me.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Paul Clark

    (@pdclark)

    Assuming the name of the custom taxonomy is like_a_tag:

    Using a Pods Single Item block, input this Custom Template:

    <div>
    [each like_a_tag]
        <span>{@name}</span>
    [/each]
    </div>

    Alternatively, with links to the taxonomy archives:

    <div>
    [each like_a_tag]
        <span><a href="{@permalink}">{@name}</a></span>
    [/each]
    </div>

    Alternatively, as a shortcode which could be used in a Paragraph block or similar content blocks, a Shortcode block, or within a Pods Custom Template if define( 'PODS_SHORTCODE_ALLOW_SUB_SHORTCODES', true ); is added to wp-config.php:

    [pods-like-a-tag]

    <?php
    
    /**
     * [pods-like-a-tag]
     * 
     * Output linked terms from taxonomy 'like_a_tag' using a pods() object.
     */
    add_shortcode(
    	'pods-like-a-tag',
    	function( $atts = [], $content = '', $tagname = '' ) {
    		// Causes output to be stored to a variable.
    		ob_start();
    
    		// pods() defaults to the current object ID and post type.
    		$pod = pods();
    		// 'like_a_tag' is the name of the taxonomy.
    		$terms = (array) $pod->field( 'like_a_tag' );
    
    		if ( ! empty( $terms ) ) {
    			// If there are terms set.
    			echo '<div>';
    			foreach( $terms as $term ) {
    				/**
    				 * printf() takes a template for the first argument,
    				 * where %s or %d are filled in with the
    				 * following arguments in order of appearance.
    				 * 
    				 * pods()->field() returns an array for $term.
    				 */
    				printf(
    					'<span><a href="%s">%s</a></span> ',
    					esc_url( get_term_link( (int) $term['term_id'] ) ),
    					esc_html( $term['name'] )
    				);
    			}
    			echo '</div>';
    		}else {
    			// If there are no terms set.
    			echo '<div>No tags.</div>';
    		}
    
    		// Returns stored output, as is required for a shortcode.
    		return ob_get_clean();
    	}
    );

    Finally, here is a similar shortcode using WordPress core functions instead of a pods() object:

    [like-a-tag]

    <?php
    
    /**
     * [like-a-tag]
     * 
     * Output linked terms from taxonomy 'like_a_tag' using WordPress core function wp_get_post_terms().
     */
    add_shortcode(
    	'like-a-tag',
    	function( $atts = [], $content = '', $tagname = '' ) {
    		// Causes output to be stored to a variable.
    		ob_start();
    
    		// 'like_a_tag' is the name of the taxonomy.
    		$terms = (array) wp_get_post_terms( get_the_ID(), 'like_a_tag' );
    
    		if ( ! empty( $terms ) ) {
    			// If there are terms set.
    			echo '<div>';
    			foreach( $terms as $term ) {
    				/**
    				 * printf() takes a template for the first argument,
    				 * where %s or %d are filled in with the
    				 * following arguments in order of appearance.
    				 * 
    				 * wp_get_post_terms() returns a WP_Term object for $term.
    				 */
    				printf(
    					'<span><a href="%s">%s</a></span> ',
    					esc_url( get_term_link( (int) $term->term_id ) ),
    					esc_html( $term->name )
    				);
    			}
    			echo '</div>';
    		}else {
    			// If there are no terms set.
    			echo '<div>No tags.</div>';
    		}
    
    		// Returns stored output, as is required for a shortcode.
    		return ob_get_clean();
    	}
    );

    References:

    Thread Starter ryo_relic

    (@ryo_relic)

    Hi Paul.

    Thanks for the reply. It helped me a lot.
    They all worked.

    The third and fourth methods for creating shortcodes don’t work in the query loop.
    I get a “No tags” message.

    One more thing I would like to know.
    Can I make the “like_a_tag” I created in pods be like the core wordpress tags?
    In other words, it would be nice if I could have the theme recognize them as core WordPress tags and use the theme’s tag sorting functionality in archive page.
    Of course, then we could just use the normal WordPress core tags. But sometimes I want to keep them separate for management purposes.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to display Custom Taxonomy in query loop’ is closed to new replies.