Funzione per recuperare categoria primaria di YOAST

Ecco la funzione pronta all’uso spero possa essere utile, potete usarla sia all’interno del function, che dentro pagine, loop etc.

Codice step-by-step

Per prima cosa recuperiamo la categoria all’interno del loop in cui ci troviamo:

$category = get_the_category();

Per interrogare Yoast usiamo la classe WPSEO_Primary_Term (se esiste), altrimenti recuperiamo la prima categoria settata.

                 if ( class_exists('WPSEO_Primary_Term') )
					{
						$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
						$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
						$term = get_term( $wpseo_primary_term );
						if (is_wp_error($term)) {
							$category_display = $category[0]->name;
							$category_link = get_category_link( $category[0]->term_id );
						} else {
							$category_display = $term->name;
							$category_link = get_category_link( $term->term_id );
						}
					}
					else {
						$category_display = $category[0]->name;
						$category_link = get_category_link( $category[0]->term_id );
					}

Infine ora che abbiamo sia il nome (category_display), sia il link(category_link) stampiamo l’html se è presente la categoria. 

                 // Display category
					if ( !empty($category_display) ){
					    if ( $useCatLink == true && !empty($category_link) ){
						$html_category = '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
					    } else {
						$html_category = htmlspecialchars($category_display);
					    }
					}

Codice completo

         // Funzione per recuperare categoria primaria di Yoast
				$category = get_the_category();
				$useCatLink = true;
				if ($category){
					$category_display = '';
					$category_link = '';
					if ( class_exists('WPSEO_Primary_Term') )
					{
						$wpseo_primary_term = new WPSEO_Primary_Term( 'category', get_the_id() );
						$wpseo_primary_term = $wpseo_primary_term->get_primary_term();
						$term = get_term( $wpseo_primary_term );
						if (is_wp_error($term)) {
							$category_display = $category[0]->name;
							$category_link = get_category_link( $category[0]->term_id );
						} else {
							$category_display = $term->name;
							$category_link = get_category_link( $term->term_id );
						}
					}
					else {
						$category_display = $category[0]->name;
						$category_link = get_category_link( $category[0]->term_id );
					}
					// Display category
					if ( !empty($category_display) ){
					    if ( $useCatLink == true && !empty($category_link) ){
						$html_category = '<a href="'.$category_link.'">'.htmlspecialchars($category_display).'</a>';
					    } else {
						$html_category = htmlspecialchars($category_display);
					    }
					}
				}//if category

Potete poi facilmente richiamarla dove desiderate all’interno del vostro template in questo modo:

<?php echo $html_category; ?>

Questo articolo ti è stato utile? Condividilo o fammi sapere cosa ne pensi nei commenti ! 🙂

Lascia un commento

Il tuo indirizzo email non sarà pubblicato. I campi obbligatori sono contrassegnati *

Questo sito usa Akismet per ridurre lo spam. Scopri come i tuoi dati vengono elaborati.

wordpress-guida
Up Next:

Galleria fotografica senza refresh con cambio url html5 per Wordpress

Galleria fotografica senza refresh con cambio url html5 per Wordpress