Skip to content

Commit

Permalink
Added more taxonomies
Browse files Browse the repository at this point in the history
  • Loading branch information
NeaMitika committed Nov 10, 2020
1 parent 83734a5 commit 6a1d628
Show file tree
Hide file tree
Showing 3 changed files with 214 additions and 35 deletions.
225 changes: 191 additions & 34 deletions admin/class-l-erbario-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ public function aggiungi_colonne_erbario( $columns ) {
'title' => __( 'Pianta', $this->plugin_name ),
'genere' => __( 'Genere', $this->plugin_name ),
'famiglia' => __( 'Famiglia', $this->plugin_name ),
'dominio' => __( 'Dominio', $this->plugin_name ),
'regno' => __( 'Regno', $this->plugin_name ),
);

return $columns;
Expand All @@ -210,44 +212,134 @@ public function aggiungi_colonne_erbario( $columns ) {

//inserisci i dati nelle colonne
public function dati_colonne_erbario( $column, $post_id ) {
// Image column
if ( $column === 'foto' ) {
if ( ! get_the_post_thumbnail() ) {
echo '<img src="' . esc_url( plugins_url( '/img/image-placeholder.jpg', __FILE__ ) ) . '" />';
}

else {
echo get_the_post_thumbnail( $post_id, array( 80, 80 ) );
}
}
switch($column) {

// Colonna Genere
if ( 'genere' === $column ) {
case 'foto' :

if (get_the_terms( get_the_ID(), 'genere' )) {
foreach ( get_the_terms( get_the_ID(), 'genere' ) as $genere ) {
echo __($genere->name) . ' ' ;
if ( ! get_the_post_thumbnail() ) {
echo '<img src="' . esc_url( plugins_url( '/img/image-placeholder.jpg', __FILE__ ) ) . '" />';
}
else {
echo get_the_post_thumbnail( $post_id, array( 80, 80 ) );
}
}

else {
echo 'n/d';
}
}

// Colonna Famiglia
if ( 'famiglia' === $column ) {
break;

case 'genere' :

$generi = get_the_terms( $post_id, 'genere' );


if ( !empty ( $generi ) ) {

$lista_generi = array();

foreach ( $generi as $genere ) {

if (get_the_terms( get_the_ID(), 'famiglia' )) {
foreach ( get_the_terms( get_the_ID(), 'famiglia' ) as $famiglia ) {
echo __($famiglia->name) . ' ' ;
$lista_generi[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'taxonomy' => $genere->taxonomy, 'post_type' => 'pianta' ), 'edit-tags.php' ) ),
esc_html( sanitize_term_field( 'name', $genere->name, $genere->term_id, 'genere', 'display' ) )
);

}

echo join( ', ', $lista_generi );
}
}

else {
echo 'n/d';
}

else{

_e( 'Nessun Genere' );

}
break;

case 'famiglia' :

$famiglie = get_the_terms( $post_id, 'famiglia' );

if ( !empty ($famiglie) ) {

$lista_famiglie = array();

foreach ( $famiglie as $famiglia ) {

$lista_famiglie[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'taxonomy' => $famiglia->taxonomy, 'post_type' => 'pianta' ), 'edit-tags.php' ) ),
esc_html( sanitize_term_field( 'name', $famiglia->name, $famiglia->term_id, 'famiglia', 'display' ) )
);

}

echo join( ', ', $lista_famiglie );
}

else {

_e( 'Nessuna Famiglia' );

}
break;

case 'dominio' :

$domini = get_the_terms( $post_id, 'dominio' );

if ( !empty ($domini) ) {

$lista_domini = array();

foreach ( $domini as $dominio ) {

$lista_domini[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'taxonomy' => $dominio->taxonomy, 'post_type' => 'pianta' ), 'edit-tags.php' ) ),
esc_html( sanitize_term_field( 'name', $dominio->name, $domini->term_id, 'dominio', 'display' ) )
);

}

echo join( ', ', $lista_domini );
}

else {

_e( 'Nessun Dominio' );

}
break;

case 'regno' :

$regni = get_the_terms( $post_id, 'regno' );

if ( !empty ($regni) ) {

$lista_regni = array();

foreach ( $regni as $regno ) {

$lista_regni[] = sprintf( '<a href="%s">%s</a>',
esc_url( add_query_arg( array( 'taxonomy' => $regno->taxonomy, 'post_type' => 'pianta' ), 'edit-tags.php' ) ),
esc_html( sanitize_term_field( 'name', $regno->name, $regno->term_id, 'regno', 'display' ) )
);

}

echo join( ', ', $lista_regni );
}

else {

_e( 'Nessun Regno' );

}
break;

default :
break;

}

}

/**
Expand Down Expand Up @@ -352,7 +444,7 @@ public function save_informazioni_aggiuntive_meta_box_data( $post_id ) {
}

/**
* Crea sezione come le categorie
* Crea categorie per Generi di piante
*/
public function create_genere_taxonomy() {

Expand All @@ -367,7 +459,7 @@ public function create_genere_taxonomy() {
'update_item' => __( 'Aggiorna Genere' ),
'add_new_item' => __( 'Aggiungi nuovo Genere' ),
'new_item_name' => __( 'Nuovo Genere' ),
'menu_name' => __( 'Genere pianta' ),
'menu_name' => __( 'Genere Pianta' ),
);

// Now register the taxonomy
Expand All @@ -383,8 +475,8 @@ public function create_genere_taxonomy() {

}

/**
* Crea sezione come le categorie
/**
* Crea categorie per Famiglie di piante
*/
public function create_famiglia_taxonomy() {

Expand Down Expand Up @@ -415,6 +507,71 @@ public function create_famiglia_taxonomy() {

}

/**
* Crea categorie per Domini di piante
*/
public function create_dominio_taxonomy() {

$labels = array(
'name' => _x( 'Dominio', 'dominio' ),
'singular_name' => _x( 'Dominio', 'dominio' ),
'search_items' => __( 'Cerca dominio' ),
'all_items' => __( 'Tutte i domini' ),
'parent_item' => __( 'Genitore dominio' ),
'parent_item_colon' => __( 'Genitore Dominio:' ),
'edit_item' => __( 'Modifica Dominio' ),
'update_item' => __( 'Aggiorna Dominio' ),
'add_new_item' => __( 'Aggiungi nuovo Dominio' ),
'new_item_name' => __( 'Nuovo Dominio' ),
'menu_name' => __( 'Dominio Pianta' ),
);

// Now register the taxonomy
register_taxonomy('dominio', array('pianta'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'dominio' ),
));

}

/**
* Crea categorie per Regni di piante
*/
public function create_regno_taxonomy() {

$labels = array(
'name' => _x( 'Regno', 'regno' ),
'singular_name' => _x( 'Regno', 'regno' ),
'search_items' => __( 'Cerca regno' ),
'all_items' => __( 'Tutte i regni' ),
'parent_item' => __( 'Genitore regno' ),
'parent_item_colon' => __( 'Genitore Regno:' ),
'edit_item' => __( 'Modifica Regno' ),
'update_item' => __( 'Aggiorna Regno' ),
'add_new_item' => __( 'Aggiungi nuovo Regno' ),
'new_item_name' => __( 'Nuovo Regno' ),
'menu_name' => __( 'Regno Pianta' ),
);

// Now register the taxonomy
register_taxonomy('regno', array('pianta'), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'regno' ),
));

}


/**
* Custom Post Template per il nostro CPT 'pianta'
*/
Expand Down
4 changes: 3 additions & 1 deletion includes/class-l-erbario.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,11 @@ private function define_admin_hooks() {
//aggiorna permalinks
$this->loader->add_action( 'init', $plugin_admin, 'controllo_aggiornamento_permalinks', 2 );

// Registra le taxonomies: Genere, Famiglia
// Registra le taxonomies: Genere, Famiglia, Dominio, Regno
$this->loader->add_action( 'init', $plugin_admin, 'create_genere_taxonomy', 1 );
$this->loader->add_action( 'init', $plugin_admin, 'create_famiglia_taxonomy', 1 );
$this->loader->add_action( 'init', $plugin_admin, 'create_dominio_taxonomy', 1 );
$this->loader->add_action( 'init', $plugin_admin, 'create_regno_taxonomy', 1 );

// Salva le informazioni dei metabox
$this->loader->add_action( 'save_post', $plugin_admin, 'save_informazioni_aggiuntive_meta_box_data' );
Expand Down
20 changes: 20 additions & 0 deletions public/templates/single-pianta.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,26 @@
}
?>
</ul>

<ul>
<?php
if (get_the_terms( get_the_ID(), 'dominio' )) {
foreach ( get_the_terms( get_the_ID(), 'dominio' ) as $dominio ) {
echo '<li>' . __( $dominio->name ) . '</li>';
}
}
?>
</ul>

<ul>
<?php
if (get_the_terms( get_the_ID(), 'regno' )) {
foreach ( get_the_terms( get_the_ID(), 'regno' ) as $regno ) {
echo '<li>' . __( $regno->name ) . '</li>';
}
}
?>
</ul>


</body>
Expand Down

0 comments on commit 6a1d628

Please sign in to comment.