diff --git a/templates/selectidp-links.php b/templates/selectidp-links.php
index 9791882917fd967f95fe75f27e3633ef504bf2ff..6c4eabad3c5fbccb57f33671b5c0ce8b2e693fce 100644
--- a/templates/selectidp-links.php
+++ b/templates/selectidp-links.php
@@ -40,30 +40,33 @@ foreach ($this->data['idplist'] as $idpentry) {
                 $this->t('idpname_'.$idpentry2['entityid'])
             );
         });
-        if (!empty($this->data['preferredidp']) &&
-            array_key_exists($this->data['preferredidp'], $this->data['idplist'])
-        ) {
-            $idpentry = $this->data['idplist'][$this->data['preferredidp']];
-            echo '<div class="preferredidp">';
-            echo '	<img src="/'.$this->data['baseurlpath'].
-                'resources/icons/experience/gtk-about.64x64.png" class="float-r" alt="'.
-                $this->t('icon_prefered_idp').'" />';
 
-            if (array_key_exists('icon', $idpentry) && $idpentry['icon'] !== null) {
-                $iconUrl = \SimpleSAML\Utils\HTTP::resolveURL($idpentry['icon']);
-                echo '<img class="float-l" style="margin: 1em; padding: 3px; border: 1px solid #999" src="'.
-                    htmlspecialchars($iconUrl).'" />';
-            }
-            echo "\n".'	<h3 style="margin-top: 8px">'.
-                htmlspecialchars($this->t('idpname_'.$idpentry['entityid'])).'</h3>';
+        if (!empty($this->data['preferredidp'])) {
+            foreach ($this->data['idplist'] as $idpentry) {
+                if ($idpentry['entityid'] != $this->data['preferredidp']) {
+                    continue;
+                }
+                echo '<div class="preferredidp">';
+                echo '	<img src="/'.$this->data['baseurlpath'].
+                    'resources/icons/experience/gtk-about.64x64.png" class="float-r" alt="'.
+                    $this->t('icon_prefered_idp').'" />';
 
-            if (!empty($idpentry['description'])) {
-                echo '	<p>'.htmlspecialchars($this->t('idpdesc_'.$idpentry['entityid'])).'<br />';
+                if (array_key_exists('icon', $idpentry) && $idpentry['icon'] !== null) {
+                    $iconUrl = \SimpleSAML\Utils\HTTP::resolveURL($idpentry['icon']);
+                    echo '<img class="float-l" style="margin: 1em; padding: 3px; border: 1px solid #999" src="'.
+                        htmlspecialchars($iconUrl).'" />';
+                }
+                echo "\n".'	<h3 style="margin-top: 8px">'.
+                    htmlspecialchars($this->t('idpname_'.$idpentry['entityid'])).'</h3>';
+
+                if (!empty($idpentry['description'])) {
+                    echo '	<p>'.htmlspecialchars($this->t('idpdesc_'.$idpentry['entityid'])).'<br />';
+                }
+                echo('<button id="preferredidp" type="submit" class="btn" name="idp_'.
+                    htmlspecialchars($idpentry['entityid']).'">'.
+                    $this->t('select').'</button></p>');
+                echo '</div>';
             }
-            echo('<button id="preferredidp" type="submit" class="btn" name="idp_'.
-                htmlspecialchars($idpentry['entityid']).'">'.
-                $this->t('select').'</button></p>');
-            echo '</div>';
         }
 
         foreach ($this->data['idplist'] as $idpentry) {