0

我创建了一个带有多个选择项的元框,它从自定义帖子类型中获取其值。

add_action("add_meta_boxes", "palinsesto_box");

function palinsesto_box() {
add_meta_box("palinsesto-meta", "Speakers",   "palinsesto_manager_meta_options", "palinsesto",   "side");}

function palinsesto_manager_meta_options($post)
{
wp_nonce_field( 'radio_schedule', 'schedule_noncename' );
echo '<label for="speaker_id">';
_e("Speaker", 'speaker_id' );
echo '</label> ';
$args = array( 'post_type' => 'speaker');
$loop = new WP_Query( $args );
echo '<select name="speaker_id[]" id="speaker_id" multiple="multiple">';
foreach($loop->posts as $dj):
    if($dj->ID == get_post_meta( $post->ID, 'speaker_id', true ))
    {
        $select = 'selected';
    }else{
    $select = '';
}
echo '<option value="'.$dj->ID.'" '.$select.'>'.$dj->post_title.'</option>';
endforeach;
echo '</select>';
echo '<p>Tieni premuto CTRL per selezionare più speakers</p>';
}

这部分有效!现在我将保存选定的值并将其显示在自定义列中,这里有一些磨损。

add_action('save_post', 'save_palinsesto_manager_meta_options');
function save_palinsesto_manager_meta_options($post_id)
{global $post;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
    //if you remove this the sky will fall on your head.
    return;
}else{
    if( isset( $_POST['speaker_id'] ) ) {  
    update_post_meta( $post_id,'speaker_id', esc_attr( $_POST['speaker_id'] ) );  
}
}
}

add_filter('manage_palinsesto_posts_columns', 'columns_palinsesto');
function columns_palinsesto($old_columns)
{
$new_columns = array(
    'cb'     => '<input type="checkbox">',
    'img'    => 'Immagine',
    'title'  => __('Palinsesto'),
    'conduce' => 'Conduce',

);
return array_merge( $new_columns, $old_columns );
}

add_action('manage_palinsesto_posts_custom_column', 'get_palinsesto_columns',
10, 2);
function get_palinsesto_columns($col, $post_id)
{global $post;

$conduce=get_post_meta( $post_id,'speaker_id',$value );


switch($col) {
case 'img':
    if(has_post_thumbnail($post_id)) {
        echo get_the_post_thumbnail($post_id);
    } else {
        echo 'Nessuna immagine!';
    }
    break;
case 'conduce':

    echo $conduce;
    break;
}
}

这部分代码只返回一个值并显示单词数组或数字....你能帮帮我吗????

4

1 回答 1

2

我建议您阅读:

http://codex.wordpress.org/Function_Reference/add_meta_box http://codex.wordpress.org/Plugin_API/Action_Reference/manage_ $post_type_posts_custom_column

我知道它正在工作的元框、保存和检索选项:

add_action("add_meta_boxes", "palinsesto_box");

function palinsesto_box() {
    add_meta_box("palinsesto-meta", "Speakers",   "palinsesto_manager_meta_options", "post", "side", 'high' );
}

function palinsesto_manager_meta_options( $post ) {

    wp_nonce_field( 'palinsesto_manager_meta_options', 'palinsesto_manager_meta_options_nonce' );

    echo '<label for="speaker_id">';
        _e("Speaker", 'speaker_id' );
    echo '</label> ';
    $args = array( 'post_type' => 'post');
    $loop = new WP_Query( $args ); 


    $speaker_id_values = get_post_meta( $post->ID, '_speaker_ids', true ); ?>

    <?php if ( $loop->have_posts() ): ?>

        <select name="speaker_id[]" id="speaker_id" multiple="multiple">

            <?php while( $loop->have_posts() ): $loop->the_post();  ?>
                <?php 
                    $selected = ( in_array( get_the_ID(), $speaker_id_values ) ) ? 'selected="selected"' :  '';
                ?>
                <option value="<?php the_ID() ?>" <?php echo $selected; ?>><?php the_title(); ?></option>

            <?php endwhile; ?>
        </select>

        <p>Tieni premuto CTRL per selezionare più speakers</p>

    <?php endif ?>

   <?php
}
add_action('save_post', 'save_palinsesto_manager_meta_options');
function save_palinsesto_manager_meta_options($post_id) {


    // Check if our nonce is set.
    if ( ! isset( $_POST['palinsesto_manager_meta_options_nonce'] ) )
        return $post_id;

    $nonce = $_POST['palinsesto_manager_meta_options_nonce'];

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $nonce, 'palinsesto_manager_meta_options' ) )
        return $post_id;

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return $post_id;

    // Check the user's permissions.
  if ( 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) )
        return $post_id;

  } else {

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;

  }


    if ( isset( $_POST['speaker_id'] ) ) {

        $sanitized_data = array();

        $data = (array) $_POST['speaker_id'];

        foreach ($data as $key => $value) {

            $sanitized_data[ $key ] = (int)strip_tags( stripslashes( $value ) );

        }

        update_post_meta( $post_id, '_speaker_ids', $sanitized_data );

    }

}

但是我没有测试的自定义列。

add_filter( 'manage_edit-speaker_columns', 'set_custom_edit_speaker_columns' );
add_action( 'manage_speaker_posts_custom_column' , 'custom_speaker_column', 10, 2 );

function set_custom_edit_speaker_columns($columns) {
    unset( $columns['author'] );
    $columns['speaker_id'] = __( 'Speaker', 'your_text_domain' );

    return $columns;
}

function custom_speaker_column( $column, $post_id ) {
    switch ( $column ) {

        case 'speaker_id' :

            $speaker_ids = get_post_meta( $post_id, '_speaker_ids', true )

            if ( count( $speaker_ids ) > 0 ) {
                foreach ( $speaker_ids as $speaker_id ) {
                     echo get_the_title($speaker_id) . ' ';   
                }    
            } else {
                echo 'Nothing here';
            }

            break;

    }
}

您可以改进代码。

于 2013-11-06T00:25:41.213 回答