WordPress : Imprimer les données de la métabox dans le pied de page ?

Publié par Jean-Michel le

J’ai suivi le tutoriel ici http://wp.smashingmagazine.com/2011/10/04/create-custom-post-meta-boxes-wordpress/

et je l’ai fait fonctionner comme prévu dans le tutoriel, mais au lieu d’ajouter la classe, je veux juste ajouter le contenu au fichier footer.php :

Voici le code du tutoriel que j’ai dans functions.php

/* Fire our meta box setup function on the post editor screen. */
add_action( 'load-post.php', 'smashing_post_meta_boxes_setup' );
add_action( 'load-post-new.php', 'smashing_post_meta_boxes_setup' );


/* Meta box setup function. */
function smashing_post_meta_boxes_setup() {

    /* Add meta boxes on the 'add_meta_boxes' hook. */
    add_action( 'add_meta_boxes', 'smashing_add_post_meta_boxes' );

    /* Save post meta on the 'save_post' hook. */
    add_action( 'save_post', 'smashing_save_post_class_meta', 10, 2 );
}


/* Create one or more meta boxes to be displayed on the post editor screen. */
function smashing_add_post_meta_boxes() {

    add_meta_box(
        'smashing-post-class',          // Unique ID
        esc_html__( 'Post Class', 'example' ),      // Title
        'smashing_post_class_meta_box',     // Callback function
        'post',                 // Admin page (or post type)
        'side',                 // Context
        'default'                   // Priority
    );
}

/* Display the post meta box. */
function smashing_post_class_meta_box( $object, $box ) { ?>

    <?php wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' ); ?>

    <p>
        <label for="smashing-post-class"><?php _e( "Add a custom CSS class, which will be applied to WordPress' post class.", 'example' ); ?></label>
        <br />
        <input class="widefat" type="text" name="smashing-post-class" id="smashing-post-class" value="<?php echo esc_attr( get_post_meta( $object->ID, 'smashing_post_class', true ) ); ?>" size="30" />
    </p>
<?php }


/* Save the meta box's post metadata. */
function smashing_save_post_class_meta( $post_id, $post ) {

    /* Verify the nonce before proceeding. */
    if ( !isset( $_POST['smashing_post_class_nonce'] ) || !wp_verify_nonce( $_POST['smashing_post_class_nonce'], basename( __FILE__ ) ) )
        return $post_id;

    /* Get the post type object. */
    $post_type = get_post_type_object( $post->post_type );

    /* Check if the current user has permission to edit the post. */
    if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
        return $post_id;

    /* Get the posted data and sanitize it for use as an HTML class. */
    $new_meta_value = ( isset( $_POST['smashing-post-class'] ) ? sanitize_html_class( $_POST['smashing-post-class'] ) : '' );

    /* Get the meta key. */
    $meta_key = 'smashing_post_class';

    /* Get the meta value of the custom field key. */
    $meta_value = get_post_meta( $post_id, $meta_key, true );

    /* If a new meta value was added and there was no previous value, add it. */
    if ( $new_meta_value && '' == $meta_value )
        add_post_meta( $post_id, $meta_key, $new_meta_value, true );

    /* If the new meta value does not match the old value, update it. */
    elseif ( $new_meta_value && $new_meta_value != $meta_value )
        update_post_meta( $post_id, $meta_key, $new_meta_value );

    /* If there is no new meta value but an old value exists, delete it. */
    elseif ( '' == $new_meta_value && $meta_value )
        delete_post_meta( $post_id, $meta_key, $meta_value );
}


/* Filter the post class hook with our custom post class function. */
add_filter( 'post_class', 'smashing_post_class' );

function smashing_post_class( $classes ) {

    /* Get the current post ID. */
    $post_id = get_the_ID();

    /* If we have a post ID, proceed. */
    if ( !empty( $post_id ) ) {

        /* Get the custom post class. */
        $post_class = get_post_meta( $post_id, 'smashing_post_class', true );

        /* If a post class was input, sanitize it and add it to the post class array. */
        if ( !empty( $post_class ) )
            $classes[] = sanitize_html_class( $post_class );
    }

    return $classes;
}

comment puis-je simplement renvoyer les données saisies dans le champ à mon footer.php sans rien de plus ?

<?--php global $post; echo get_post_meta($post->ID,'smashing-post-class',true) ?>
</body>
</html>

Solution n°1 trouvée

Dans votre footer.php, vous pouvez appeler global $postpour accéder aux informations qui y sont stockées. Par exemple:

<?--php
global $post;
echo get_post_meta($post->ID,'smashing-post-class',true) ?>

Catégories : Wordpress

Jean-Michel

Jean-Michel est en charge de la partie blog du site. Il met en place la stratégie de contenu et répond aux questions fréquentes sur Wordpress.

0 commentaire

Laisser un commentaire

Avatar placeholder

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *