0

I have created a meta box which has a select dropdown list and i want to show it in the post as a user selects a particular option from meta box.I cant figure it out how its done.Here is my meta box code.

<?php
function display_post_options(){
    global $post;
    wp_nonce_field( basename( __FILE__ ), 'post_options_nonce' ); 
    $post_options_select_value =  get_post_meta($post->ID,'post_options_select',true);

?>



    <label for="options-select">Choose Your Post Layout:</label>
    <select name='post_options_select' id="options-select">
        <option id='right-sidebar' value='right' name='right_sidebar' <?php selected($post_options_select_value, 'right'); ?> >Right sidebar</option>
        <option id='left-sidebar' value="left" name='left_sidebar' <?php selected($post_options_select_value, 'left'); ?>>Left Sidebar</option>
        <option id='no-sidebar' value="no" name='no_sidebar' <?php selected($post_options_select_value, 'no'); ?>>No Sidebar</option>
    </select>


<?php
}

function post_options(){
    add_meta_box( 
        'post-options',
        'Post Options',
        'display_post_options',
        'post',
        'advanced',
        'high',
        $callback_args
    );
}

function save_post_options($post_id){

    if (if_user_can_save($post_id, 'post_options_nonce')) {
        update_post_meta($post_id, 'post_options_select', $_POST['post_options_select']);
    }

}

function if_user_can_save($post_id, $nonce){

    //Check if post is autosave
    $is_autosave = wp_is_post_autosave($post_id);
    //Check if it is a Revision
    $is_revision = wp_is_post_revision($post_id);
    //Is the nonce valid
    $is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], basename( __FILE__ ) ) );
    return !($is_autosave || $is_revision) && $is_valid_nonce;

}

function display_content( $content ){
    $test = get_post_meta(get_the_ID(), 'options-select', true);
    $content .= $test;
    print_r($test);
    return $content;
}

add_action('add_meta_boxes', 'post_options');
add_action( 'save_post', 'save_post_options', 10, 2 );
add_action('the_content', 'display_content');
?> 
1

Your meta option is post_options_select. You are using wrong meta key options-select in function display_content.

But main point is that, you cannot control the sidebar layout from the_content filter. If selected option is 'No Sidebar' then you need to hide sidebar in your template. the_content only contains the content of the post. How it should be displayed whether with sidebar or not, it should be controlled while including sidebar.

  • i know i cant do that with the content but if i have the values i can change the css dynamically with redux framework.if your have better alternative for that please let me know. – user3443160 Aug 27 '14 at 7:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.