0

I got two taxonomies: "Colours" and "Recommended Colour" both have the same terms (same colours).

I want to display a list of all terms of the taxonomy "Colours" and mark one colour as recommended. So using the following foreach I get the selected terms of the taxonomy " the selected colours"

      <?php
        $colours = wp_get_post_terms($post->ID, 'colours', array("fields" => "all"));
        foreach($colours as $colour) {
          $colour_class = $colours->slug;
          $colour_title = $carcase_single->name;?>
          <div class="<?php echo $colour_class ?>" title="<?php echo $colour_title;?>"></div>
      <?php  }
      ?>

and with this foreach I get the term selected on the "Recommended Colour" Taxonomy (recommended colour):

<?php
  $recommended_colours = wp_get_post_terms($post->ID, 'recommended_colours', array("fields" => "all"));
  foreach($recommended_colours as $recommended_colour) {
    $recommended_colour_class = $recommended_colour->slug; ?>
    <div class="<?php echo $recommended_colour_class ?>" title="<?php echo $colour_title;?>"></div>
<?php } ?>

Now I want to join both foreach and display all selected colours with the recommend colour.

I 'joined' both foreach but only I get the recommended colour and not all selected colours and the recommended one with this code:

<?php
  $colours = wp_get_post_terms($post->ID, 'colours', array("fields" => "all"));
  $recommended_colours = wp_get_post_terms($post->ID, 'recommended_colours', array("fields" => "all"));

  foreach($colours as $colour) {
    $colour_class = $colours->slug;
    $colour_title = $carcase_single->name; }
    foreach($recommended_colours as $recommended_colour) {
      $recommended_colour_class = $recommended_colour->slug;  }
?>
    <div class="<?php echo $colour_class $recommended_colour_class ?>" title="<?php echo $colour_title;?>"></div>

I want to get the following:

<div class="<?php echo $colour_class  ?>" title="<?php echo $colour_title;?>"></div>
<div class="<?php echo $colour_class $recommended_colour_class ?>" title="<?php echo $colour_title;?>"></div>
<div class="<?php echo $colour_class ?>" title="<?php echo $colour_title;?>"></div>
<div class="<?php echo $colour_class ?>" title="<?php echo $colour_title;?>"></div>
<div class="<?php echo $colour_class ?>" title="<?php echo $colour_title;?>"></div>

Thank you!

2

First, I'd extract all the slugs of the recommended colour terms with something like this:

$recommended_colours = wp_get_post_terms($post->ID, 'recommended_colours');
$recommended_colour_slugs = array_map(function($colour) {
    return $colour->slug;
}, $recommended_colours);

... which leaves you with a $recommended_colour_slugs array that looks like this ['blue', 'green'].

Then you can loop over your colour terms and use an if-else to check whether the current colour exists in the $recommended_colour_slugs array and set the css class for that item accordingly.

$colours = wp_get_post_terms($post->ID, 'colours');

foreach($colours as $colour) :
    $colour_class = $colour->slug;
    $colour_title = $colour->name;
    if (in_array($colour->slug, $recommended_colour_slugs)) {
        $colour_class .= ' recommended';
    }
    ?>
    <div class="<?php echo $colour_class; ?>" title="<?php echo $colour_title;?>"></div>
<?php endforeach;
| improve this answer | |
  • Hi Martin Sotirov, Thanks for your replay but I don't get it at all... Could you explain using another way, please? Please take in mind that my php knowledge is equal to 0... Thanks! – andresgl Apr 29 '16 at 12:00
  • I've updated my code snippets with more details. Basically, you have two arrays of terms and you want to see if there's any overlap between contents of those arrays. The array_map function in my snippet creates a new array containing just the slugs (names) of recommended colours and then in the foreach loop below it checks every colour to see if it's featured in the recommended colours array. Also, take note that you have to concatenate your variables using . (dot) when echoing them out. – Martin Sotirov Apr 29 '16 at 12:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.