Term query of custom product taxonomy

Home » Snippets » Term query of custom product taxonomy
0

Created with:

Visibility: 

public

Creator: silvano

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php

$args = array(
    'taxonomy' => 'tax_artista',
    'fields'   => 'all',
    'number'   => 10,
    'paged'    => ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1,
);

$term_query = new WP_Term_Query( $args );

if ( ! empty( $term_query->terms ) ) {
    
    foreach ( $term_query->terms as $term ) {
        
        $foto_artista = get_field( 'foto_artista', 'term_' . $term->term_id );
        
        if ( $foto_artista ) {
            echo '<h2>' . $term->name . '</h2>';
            echo '<img src="' . esc_url( $foto_artista['url'] ) . '" alt="' . esc_attr( $term->name ) . '">';
        } else {
            // Use the URL of the fallback image
            $fallback_image_url = 'https://example.com/path/to/fallback-image.jpg';
            echo '<h2>' . $term->name . '</h2>';
            echo '<img src="' . esc_url( $fallback_image_url ) . '" alt="' . esc_attr( $term->name ) . '">';
        }
    }
}
?>
				

Custom term query to show Name term and Image term.

Image add with ACF field

Register an account to save your snippets or go Pro to get more features.