Skip to content

Instantly share code, notes, and snippets.

@willybahuaud
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willybahuaud/b82774913e23ba7aa6a0 to your computer and use it in GitHub Desktop.
Save willybahuaud/b82774913e23ba7aa6a0 to your computer and use it in GitHub Desktop.
<?php
// Ajout de metabox dans le BO
add_action("add_meta_boxes", "willy_create_metaboxes", 10, 2);
function willy_create_metaboxes( $post_type, $post ) {
if ( 'product' == $post_type ) {
add_meta_box("artiste","Artiste","artiste_zik", $post_type,"side","high");
}
}
add_action('save_post', 'willy_save_post');
function willy_save_post( $post_ID ){
// on retourne rien du tout s'il s'agit d'une sauvegarde automatique
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return $post_ID;
if ( isset( $_POST['artiste'] ) ) {
check_admin_referer('artiste-save_'.$_POST['post_ID'], 'artiste-nonce') ;
update_post_meta( $_POST['post_ID'], '_artiste', intval( $_POST['artiste'] ) );
}
}
// La metabox
function artiste_zik( $post ) {
$artisteEnregistre = get_post_meta( $post->ID, '_artiste', true );
wp_nonce_field( 'artiste-save_' . $post->ID, 'artiste-nonce');
$artistes = get_posts( array( 'post_type' => 'artistes', 'numberposts' => -1 ) );
if ( $artistes ) {
echo '<p><label for="select-artiste" style="width:100%;display:block;">Auteur de l\'oeuvre :</label>';
echo '<select name="artiste" id="select-artiste" style="width:100%;display:block;">';
echo '<option value="">Indéfini</option>';
foreach ( $artistes as $artiste ) {
echo '<option value="' . $artiste->ID . '" ' . selected( $artiste->ID, $artisteEnregistre, false ) . '>' . sanitize_text_field( $artiste->post_title ) . '</option>';
}
echo '</select><p>';
/*if ( $artisteEnregistre ) {
echo '<table class="widefat fixed" cellspacing="0"><tbody>';
$infos = array();
if ( $tel = get_post_meta( $artisteEnregistre, 'tel', true ) ) {
$infos['Téléphone'] = sanitize_text_field( $tel );
}
if ( $email = get_post_meta( $artisteEnregistre, 'adresse_email', true ) ) {
$infos['Email'] = '<a href="mailto:' . sanitize_email( $email ) . '">' . sanitize_email( $email ) . '</a>';
}
if ( $adresse = get_post_meta( $artisteEnregistre, 'adresse_postale', true ) ) {
$infos['Adresse'] = nl2br( $adresse );
}
$i = 0;
foreach( $infos as $k => $info ) {
echo '<tr' . ( $i++%2 == 0 ? ' class="alternate"' : '' ) . '>';
echo '<th style="padding-right:1em;vertical-align:top;">' . $k . '</th>';
echo '<td>' . $info . '</td>';
echo '</tr>';
}
echo '</tbody></table>';
}*/
} else {
echo '<p>Aucun artiste n\'existe actuellement.</p>';
}
echo '<p><a href="' . admin_url( 'post-new.php?post_type=artistes' ) . '">' . __( 'Créer un nouvel artiste', 'nebula' ) . '</a></p>';
}
// Fonction à appeler dans le template
function getTheArtiste() {
global $post;
$artiste = get_post_meta( $post->ID, '_artiste', true );
if ( $artiste ) {
$artiste = new WP_Query( array( 'post_type' => 'artistes', 'posts_per_page' => 1, 'post__in' => array( $artiste ) ) );
if ( $artiste->have_post() ) : $artiste->the_post();
// Choppe ce que tu veux sur l'artiste
echo '<a href="' . get_permalink( $artiste->post->ID ) . '">' . the_title( '', '', false ) . '</a>';
endif;
wp_reset_postdata();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment