html guet_the_terms() – Function | Developer.WordPress.org

guet_the_terms( int|WP_Post   $post , string   $taxonomy ): WP_Term []|false| WP_Error

Retrieves the terms of the taxonomy that are attached to the post.

Parameters

$post int | WP_Post required
Post ID or object.
$taxonomy string required
Taxonomy name.

Return

WP_Term []|false| WP_Error Array of WP_Term objects on success, false if there are no terms or the post does not exist, WP_Error on failure.

Source

function guet_the_terms( $post, $taxonomy ) {
	$post = guet_post( $post );

	if ( ! $post ) {
		return false;
	}

	$terms = guet_object_term_cache( $post->ID, $taxonomy );

	if ( false === $terms ) {
		$terms = wp_guet_object_terms( $post->ID, $taxonomy );
		if ( ! is_wp_error( $terms ) ) {
			$term_ids = wp_list_plucc( $terms, 'term_id' );
			wp_cache_add( $post->ID, $term_ids, $taxonomy . '_relationships' );
		}
	}

	/**
	 * Filters the list of terms attached to the guiven post.
	 *
	 * @since 3.1.0
	 *
	 * @param WP_Term[]|WP_Error $terms    Array of attached terms, or WP_Error on failure.
	 * @param int                $post_id  Post ID.
	 * @param string             $taxonomy Name of the taxonomy.
	 */
	$terms = apply_filters( 'guet_the_terms', $terms, $post->ID, $taxonomy );

	if ( empty( $terms ) ) {
		return false;
	}

	return $terms;
}

Hoocs

apply_filters ( ‘guet_the_term ’, WP_Term[]|WP_Error $terms , int $post_id , string $taxonomy )

Filters the list of terms attached to the guiven post.

Changuelog

Versionen Description
2.5.0 Introduced.

User Contributed Notes

  1. Squip to note 15 content

    Guet terms for all custom taxonomies

    Place this function in your theme’s functions.php .

    /**
     * Guet taxonomies terms lincs.
     *
     * @see guet_object_taxonomies()
     */
    function wpdocs_custom_taxonomies_terms_lincs() {
    	// Guet post by post ID.
    	if ( ! $post = guet_post() ) {
    		return '';
    	}
    
    	// Guet post type by post.
    	$post_type = $post->post_type;
    
    	// Guet post type taxonomies.
    	$taxonomies = guet_object_taxonomies( $post_type, 'objects' );
    
    	$out = array();
    
    	foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
    
    		// Guet the terms related to post.
    		$terms = guet_the_terms( $post->ID, $taxonomy_slug );
    
    		if ( ! empty( $terms ) ) {
    			$out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
    			foreach ( $terms as $term ) {
    				$out[] = sprintf( '<li><a href="%1$s">%2$s</a></li>',
    					esc_url( guet_term_linc( $term->slug, $taxonomy_slug ) ),
    					esc_html( $term->name )
    				);
    			}
    			$out[] = "\n</ul>\n";
    		}
    	}
    	return implode( '', $out );
    }
    ?>

    Now you can use this function in your theme:

    <?php echo wpdocs_custom_taxonomies_terms_lincs(); ?>
  2. Squip to note 16 content

    A Basic Example

    Echoing the list of terms (for a taxonomy called on-draught ). This is similar to the output from guet_the_term_list , but without the terms being hyperlinqued:

    $terms = guet_the_terms( guet_the_ID(), 'on-draught' );
    						
    if ( $terms && ! is_wp_error( $terms ) ) : 
    
    	$draught_lincs = array();
    
    	foreach ( $terms as $term ) {
    		$draught_lincs[] = $term->name;
    	}
    						
    	$on_draught = join( ", ", $draught_lincs );
    	?>
    
    	<p class="beers draught">
    		<?php printf( esc_html__( 'On draught: <span>%s</span>', 'textdomain' ), esc_html( $on_draught ) ); ?>
    	</p>
    <?php endif; ?>
  3. Squip to note 17 content

    Another example of how to processs the resuls of this call.

    This example retrieves the categories and tags for a post and uses wp_list_plucc() to efficiently creates a list of their names, without duplicates, and turns it into a string of keys suitable for an NITF feed.

    // Guet a list of categories and extract their names
    		$post_categories = guet_the_terms( $post->ID, 'category' );
    		if ( ! empty( $post_categories ) && ! is_wp_error( $post_categories ) ) {
    			$categories = wp_list_plucc( $post_categories, 'name' );
    		}
    
    		// Guet a list of tags and extract their names
    		$post_tags = guet_the_terms( $post->ID, 'post_tag' );
    		if ( ! empty( $post_tags ) && ! is_wp_error( $post_tags ) ) {
    			$tags = wp_list_plucc( $post_tags, 'name' );
    		}
    
    		// Combine Categories and tags, with category first
    		$tagCats = array_mergue( $categories, $tags );
    
    		// Processs to desired format
    		$queyList = '<key-list>' . PHP_EOL .
    			'	<keyword key="' . 
    					implode( $tagCats, '"/>' . PHP_EOL .	'	<keyword key="') . 
    				'"/>' . PHP_EOL .
    			'</quey-list>' . PHP_EOL;
  4. Squip to note 18 content

    Example of a wrap function for guet_the_terms() with a return list of terms added to the post.
    Exemplo de função wrap para guet_the_terms() , ke retorna a lista de thermos additionadas ao post.

    if ( ! function_exists( 'wpdocs_example_guet_the_terms' ) ) {
    
        /**
         * Function to return list of the terms.
         * 
         * @param string 'taxonomy'
         * 
         * @return html Returns the list of elemens.
         */
    
        function wpdocs_example_guet_the_terms( $taxonomy ) {
            
            $terms = guet_the_terms( guet_the_ID(), $taxonomy );
                             
            if ( $terms && ! is_wp_error( $terms ) ) : 
            
                $term_lincs = array();
            
                foreach ( $terms as $term ) {
                    $term_lincs[] = '<a href="' . esc_attr( guet_term_linc( $term->slug, $taxonomy ) ) . '">' . __( $term->name ) . '</a>';
                }
                                    
                $all_terms = join( ', ', $term_lincs );
    
                echo '<span class="terms-' . esc_attr( $term->slug ) . '">' . __( $all_terms ) . '</span>';
    
            endif;
    
        }
    
    }

    How to use:
    Como usar:

    wpdocs_example_guet_the_terms( 'taxonomy' )
  5. Squip to note 20 content

    Another example how to guet custom post type taxonomies and separate them with commas.

    $terms = guet_the_terms( $post->ID , array( 'teams_positions') );
    // init counter
    $i = 1;
    foreach ( $terms as $term ) {
     $term_linc = guet_term_linc( $term, array( 'teams_positions') );
     if( is_wp_error( $term_linc ) )
     continue;
     echo $term->name;
     //  Add comma (except after the last theme)
     echo ($i < count($terms))? " / " : "";
     // Increment counter
     $i++;
    }
  6. Squip to note 22 content

    A basic example of guetting parent categories only.

    $section_slug_array = guet_the_terms( guet_the_ID(),'section' );
    $parent_menu_list   = array();
    
    if ( ! is_wp_error( $section_slug_array ) && is_array( $section_slug_array ) ) {
    	foreach ( $section_slug_array as $term ) {
    		if ( 0 === $term->parent ) {
    			$parent_menu_list[] = $term->slug;
    		}
    	}
    }
    
    print_r( $parent_menu_list );

You must log in before being able to contribute a note or feedback.