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;                                     

Coming back to this, I’ve created a separate terms conditions for logos for the mobile version. While it is sound, the problem is that the logos for the desktop quit functioning correctly. There’s only ONE logo that displays across the board on desktop when there should be a different logo based on the taxonomy. So I’m not sure why there’s a conflict, especially since I’m using different variable terms for the desktop logos and the mobile logos. If I removed the code for the mobile logos, the desktop logos appear correctly. Vice versa if I was to remove the desktop, the mobile will load the correct logo.

I just need some happiness for both.

FOR MOBILE:

    <header>
        <div class="wsmobileheader clearfix">
            <a id="wsnavtoggle" class="animated-arrow"><span></span></a>
<?php
$termsmob = get_the_terms( get_the_ID(), array ( 'practice-areas-types', 'category' ) );
			if ( !empty($termsmob) && !is_wp_error($termsmob) ) :
			$imagemob_echoed = false; // Flag to avoid multiple echoing
			foreach ( $termsmob as $categorymob ) :
			$termmob_name = $categorymob->name;

			// Priority check for 'practice-areas-types' taxonomy first
			if ( $categorymob->taxonomy == 'practice-areas-types' ) :
			if ( $termmob_name == 'Data Breaches' ) :
			echo '<a href="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-data-breach-mob.svg" alt="Sacramento Data Breaches Lawyer Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			$imagemob_echoed = true;
			break;
			elseif ( in_array( $termmob_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="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/arnold-logo-white-2.svg" alt="Sacramento Personal Injury Lawyer Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			$imagemob_echoed = true;
			break;
			elseif ( in_array( $termmob_name, ['Employment Law','Qui Tam/ Whistleblower','Wage and Hour'] ) ) :
			echo '<a href="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-employment-attorneys-mob.svg" alt="Sacramento Employment Lawyer Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			$imagemob_echoed = true;
			break;
			elseif ( in_array( $termmob_name, ['Class Action','Product Safety','Dangerous Drugs','Data Breach'] ) ) :
			echo '<a href="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-class-action-mob.svg" alt="Sacramento Class Action Attorneys Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			$imagemob_echoed = true;
			break;
			endif;
			endif;

			// If no image has been echoed yet, check for 'category' taxonomy
			if ( !$imagemob_echoed && $categorymob->taxonomy == 'category' ) :
			if ( $termmob_name == 'Data Breach' ) :
			echo '<a href="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-data-breach-mob.svg" alt="Sacramento Data Breaches Lawyer Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			break;
			elseif ( in_array( $termmob_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="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/arnold-logo-white-2.svg" alt="Sacramento Personal Injury Lawyer Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			break;
			elseif ( in_array( $termmob_name, ['Employment Law','Qui Tam/ Whistleblower','Wage and Hour'] ) ) :
			echo '<a href="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-employment-attorneys-mob.svg" alt="Sacramento Employment Lawyer Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			break;
			elseif ( in_array( $termmob_name, ['Class Action Lawsuit', 'Product Safety', 'Dangerous Drugs', 'Data Breach'] ) ) :
			echo '<a href="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/2025/arnold-class-action-mob.svg" alt="Sacramento Class Action Attorneys Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';
			break;
			endif;
			endif;
			endforeach;
			else :
			echo '<a href="/"><span class="smallogo"><img id="site-logo" class="img-responsive" src="' . esc_url(get_template_directory_uri()) . '/assets/images/arnold-logo-white-2.svg" alt="Sacramento injury Lawyer Arnold Law Firm" width="160" style="margin-left:auto;margin-right:auto;"></span></a>';				
			endif;													
?>
            <a class="callusicon" href="tel:9167777777" onclick="ga('send', 'event', 'Phone', 'Click', 'Header');"><span class="fa fa-phone"></span><span class="screen-reader-text">Call Us</span></a>
        </div>

FOR DESKTOP:

        <div class="container-fluid shrink top-bar">
                        <div class="hidden-xs logo">
<?php
$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/arnold-data-breach-attorneys.avif" alt="Sacramento Data Breaches Lawyer Arnold Law Firm"></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/arnold-accident-injury-attorneys.avif" alt="Sacramento Personal Injury Lawyer Arnold Law Firm"></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/arnold-employment-attorneys.avif" alt="Sacramento Employment Lawyer Arnold Law Firm"></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/arnold-class-action-attorneys.avif" alt="Sacramento Class Action Attorneys Arnold Law Firm"></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/arnold-data-breach-attorneys.avif" alt="Sacramento Data Breaches Lawyer Arnold Law Firm"></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/arnold-accident-injury-attorneys.avif" alt="Sacramento Personal Injury Lawyer Arnold Law Firm"></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/arnold-employment-attorneys.avif" alt="Sacramento Employment Lawyer Arnold Law Firm"></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/arnold-class-action-attorneys.avif" alt="Sacramento Class Action Attorneys Arnold Law Firm"></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/2017/arnold-logo-new.svg" alt="Sacramento injury Lawyer Arnold Law Firm"></a>';				
endif;													
?>

Well for starters, you wouldnt need all the $image_echoed stuff; the break will short-circuit your foreach and skip the second half of the loop automatically.

Secondly, when you say there’s only one logo that displays across the board… which image? Is it possibly that your first if inside the desktop block is falling to the else clause? (Is $terms correctly set?)

Greetings, @m_hutley!

Well for starters, you wouldnt need all the $image_echoed stuff; the break will short-circuit your foreach and skip the second half of the loop automatically.

So you recommend that I dump all of the $image_echoed and the break? I don’t understand why I wouldn’t need that.

The reason why I added the break was because there are different taxonomies that require their own unique logo (both in desktop and mobile). So for each case, I wanted to ‘break’ the cycle to prevent the wrong logo posting. If you have a more viable solution, I’m all ears.

Secondly, when you say there’s only one logo that displays across the board… which image? Is it possibly that your first if inside the desktop block is falling to the else clause? (Is $terms correctly set?)

On the desktop, this image:

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>';

Anything is possible. I just know that if I completely exclude the Mobile code, the desktop works correctly. But after adding the Mobile code, the above desktop logo posts on every taxonomy (on desktop) when it shouldn’t–I should be seeing a unique logo based on taxonomy. So there is a possibility of the last statement/if/endforeach in the Mobile code that could be conflicting with the desktop code.

No, what i’m saying is this:

foreach ($a in $b) {
  if(true) {
    break;
  }
  //No need to check anything here...
  ThisFunctionWillNeverExecuteBecauseBreak();
}

I dont need to check if my if was executed; if the if was triggered, the break… broke the code out of the foreach loop immediately.

for($i = 0; $i < 5; $i++) {
  if($i == 3) { echo "I'm done"; break; }
  //I dont need to check if we're done or not.
  echo $i;
}

So your initial if:

evaluated false. So either $terms is empty, or there was a wp error. Figure out which. (At a guess; does calling get_the_terms more than once produce problems?)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.