Echo Result Based on Multiple Taxonomies Condition

I’m working on a site that uses a CPT taxonomy as well as Blog Post taxonomies (Practice Areas Types and Category). What I’m trying to do is create a conditional output that if the CPT Posts and Blog Posts fall within a condition based upon the taxonomy name, it will output an image. However, I’m encountering two issues:

(1) The CPT Posts are connected to MULTIPLE CPT taxonomies, and the Blog Posts are related to multiple Blog Post taxonomies.
(2) Most of the CPT taxonomies have the same names as the Blog Post taxonomies (please don’t ask why–I did not develop the site), and many of the taxonomy slugs are the same.
(3) The conditional code I’m creating, however, will either post two images instead of one based upon the fact that both the CPT Posts and the Blog Posts are using multiple taxonomies, and I only want ONE image to post based upon the taxonomy that is Priority. If the condition doesn’t apply to any of the post taxonomies, then the ‘else :’ kicks in with a default image.
(4) I need the correct image to post if the user visits the Blog Taxonomy archive and the CPT Taxonomy page as well.

Here is what I have thus far that is working for CPT Posts that are only assigned to ONE single taxonomy…somewhat:

<?php
$terms = get_the_terms( get_the_ID(), array ( 'practice-areas-types', 'category' ) );
if ( !empty($terms) && !is_wp_error($terms) ) :
    foreach ( $terms as $category ) :
        $term_name = $category->name;
        if ( $term_name == 'Data Breaches' ) :
            echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-data-breach-attorneys.avif" alt="Sacramento Data Breaches Lawyer Arnold Law Firm"></a>';
            break;
        elseif ( $term_name == 'Personal Injury' || 'Car Accidents' || 'Motorcycle Accidents' || 'Truck Accidents' || 'Airplane Accidents' || 'Boating Accidents' || 'Burn Injuries' || 'Catastrophic Injuries' || 'Defective Medical Products' || 'Dog Bites' || 'Drunk Driving Injuries' || 'Food Poisoning' || 'Hit and Run Accidents' || 'Paralysis' || 'Premises Liabilty' || 'Spinal Cord Injuries' || 'Traumatic Brain Injuries' || 'Wrongful Death' || 'Train Accidents' || 'Shoulder Injuries' || 'Broken Bones' || 'Wildfires' || 'Rideshare Accidents' || 'Whiplash Injuries') :
            echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-accident-injury-attorneys.avif" alt="Sacramento Personal Injury Lawyer Arnold Law Firm"></a>';
            break;
        elseif ( $term_name == 'Employment Law' ) :
            echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-employment-attorneys.avif" alt="Sacramento Employment Lawyer Arnold Law Firm"></a>';
            break;
        elseif ( $term_name == 'Class Action' || 'Product Safety' || 'Dangerous Drugs' || 'Data Breach' ) :
            echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-class-action-attorneys.avif" alt="Sacramento Class Action Attorneys Arnold Law Firm"></a>';
            break;
        else :
            echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2017/arnold-logo-new.svg" alt="Sacramento injury Lawyer Arnold Law Firm"></a>';				
        endif;
    endforeach;
		else :
            echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2017/arnold-logo-new.svg" alt="Sacramento injury Lawyer Arnold Law Firm"></a>';	
endif;												
?>

Thank you for taking a look, and I appreciate any help!

blinks slowly

Can we try that paragraph in english for the idiots in the class?

I’ma skip 1 and 2 because they arent actually issues.

So now I have to guess at what you mean, because i dont know Wordpress lingo. But i think i can guess what’s happening based on the code you provided.

You’re foreach’ing. So it’ll spit something out for each “term”. If you want only a single thing, you wouldnt foreach the terms.
What’s your definition of priority? The order in the if statement?

$priority = ['Data Breaches','Personal Injury',...(insert the rest of that line here)...,'Employment Law', 'Class Action', ....etc.];
$found = -1;
foreach ($priority AS $index => $search) {
  if (in_array($search,$terms)) { $found = $index; break; }
}
if ($found == 0) { $image = '2025/arnold-data-break-attorneys.avif'; }
elseif($found <= (Im not counting the number, but however many things are in that list)) { $image = "2025/arnold-accident-injury-attorneys.avif"; }
elseif($found == thepreviousnumberplus1) { $image = '2025/arnold-employment-attorneys.avif'; }
elseif($found <= theotherpreviousnumberplus3) { $image =  "2025/arnold-class-action-attorneys.avif"; }
else { $image = "2017/arnold-logo-new.svg"; }
echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/'.$image.'" alt="Sacramento injury Lawyer Arnold Law Firm"></a>';

the break is doing the heavy lifting here.

I did finally come to a solution:

$terms = get_the_terms( get_the_ID(), array ( 'practice-areas-types', 'category' ) );

if ( !empty($terms) && !is_wp_error($terms) ) :

    $image_echoed = false; // Flag to avoid multiple echoing
	
    foreach ( $terms as $category ) :
	
        $term_name = $category->name;
                                                    
		// Priority check for 'practice-areas-types' taxonomy first
		
		if ( $category->taxonomy == 'practice-areas-types' ) :
			if ( $term_name == 'Data Breaches' ) :
				echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/databreach-attorneys.svg" alt="Data Breaches "></a>';
				$image_echoed = true;
				break;
			elseif ( in_array( $term_name, ['Bicycle Accidents','Personal Injury','Car Accidents','Motorcycle Accidents','Truck Accidents','Airplane Accidents','Boating Accidents','Burn Injuries','Catastrophic Injuries', 'Defective Medical Products','Dog Bites','Drunk Driving Injuries','Food Poisoning','Hit and Run Accidents','Paralysis','Premises Liability','Spinal Cord Injuries','Traumatic Brain Injuries','Wrongful Death','Train Accidents','Shoulder Injuries','Broken Bones','Wildfires','Rideshare Accidents','Whiplash Injuries'] ) ) :
				echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/accidentinjury-attorneys.svg" alt="Personal Injury "></a>';
				$image_echoed = true;
				break;
			elseif ( in_array( $term_name, ['Employment Law','Qui Tam/ Whistleblower','Wage and Hour'] ) ) :
				echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/employmentattorneys.svg" alt=""></a>';
				$image_echoed = true;
				break;
			elseif ( in_array( $term_name, ['Class Action','Product Safety','Dangerous Drugs','Data Breach'] ) ) :
				echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/class-action-attorneys.svg" alt="Class Action Attorneys "></a>';
				$image_echoed = true;
				break;
			endif;
		endif;
    
		// If no image has been echoed yet, check for 'category' taxonomy
		
        if ( !$image_echoed && $category->taxonomy == 'category' ) :
            if ( $term_name == 'Data Breach' ) :
                echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/databreach-attorneys.svg" alt="Data Breaches "></a>';
                break;
            elseif ( in_array( $term_name, ['Personal Injury', 'Auto Accidents', 'Motorcycle Accident', 'Trucking Accident', 'Airplane Accidents', 'Boating Accidents', 'Burn Injuries', 'Catastrophic Injuries', 'Defective Medical Products', 'Dog Bites', 'Drunk Driving Injuries', 'Food Poisoning', 'Hit and Run Accidents', 'Paralysis', 'Premises Liabilty', 'Spinal Cord Injuries', 'Traumatic Brain Injuries', 'Wrongful Death', 'Train Accidents', 'Shoulder Injuries', 'Broken Bones', 'Wildfires', 'Rideshare Accidents', 'Whiplash Injuries'] ) ) :
                echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/accidentinjury-attorneys.svg" alt="Personal Injury "></a>';
                break;
            elseif ( in_array( $term_name, ['Employment Law','Qui Tam/ Whistleblower','Wage and Hour'] ) ) :
                echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/employmentattorneys.svg" alt=""></a>';
                break;
            elseif ( in_array( $term_name, ['Class Action Lawsuit', 'Product Safety', 'Dangerous Drugs', 'Data Breach'] ) ) :
                echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/class-action-attorneys.svg" alt="Class Action Attorneys "></a>';
                break;
            endif;
        endif;
		
    endforeach;
	
else :
	echo '<a href="/"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/accidentinjury-attorneys.svg" alt="injury "></a>';               
endif;