在wordpress元框

时间:2015-12-20 14:02:54

标签: php wordpress checkbox

我正在构建一个插件,在管理员端提供作者名称作为复选框。已检查的名称应显示在Wordpress的前端。

我已经为管理员端编写了代码,即我的代码提供了复选框,并保存了它的值。

如何在前端显示选中的值?

<?php

add_action('add_meta_boxes', 'add_custom_boxx');

function add_custom_boxx($post) {
    add_meta_box(
        'Meta Box',         // ID, should be a string.
        'Featured People',  // Meta Box Title.
        'people_meta_boxx', // Your call back function, this is where your form field will go.
        'post',             // The post type you want this to show up on, can be post, page, or custom post type.
        'side',             // The placement of your meta box, can be normal or side.
        'core'              // The priority in which this will be displayed.
    );
}

function people_meta_boxx($post) {
    wp_nonce_field('my_awesome_nonce', 'awesome_nonce');
    $checkboxMeta = get_post_meta($post->ID);

    $html = "";

    $blogusers = get_users('blog_id=1&orderby=nicename&role=author');

    // Array of WP_User objects.
    foreach ($blogusers as $user) {
?>
    <input type="checkbox"
        name="<?php echo $user->display_name;?>"
        id="<?php echo $user->display_name;?>"
        <?php if (isset($checkboxMeta[$user->display_name])) {
            checked($checkboxMeta[$user->display_name][0], 'yes'); } ?> />
    <?php echo $user->display_name;?><br />

<?php
    } // end foreach
?>

<?php } // end function people_meta_boxx

add_action('save_post', 'save_people_checkboxesx');

function save_people_checkboxesx($post_id) {
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return;

    if ((isset($_POST['my_awesome_nonce'])) && (! wp_verify_nonce($_POST['my_awesome_nonce'], plugin_basename(__FILE__))))
        return;

    if ((isset($_POST['post_type'])) && ('page' == $_POST['post_type'])) {
        if (! current_user_can('edit_page', $post_id)) {
            return;
        }
    } else {
        if (! current_user_can('edit_post', $post_id)) {
            return;
        }
    }

     $blogusers = get_users('blog_id=1&orderby=nicename&role=author');

    // Array of WP_User objects.
    foreach ($blogusers as $user) {

        //saves bob's value
        if (isset($_POST[$user->display_name])) {
            update_post_meta($post_id, $user->display_name, 'yes');
        } else {
            update_post_meta($post_id, $user->display_name, 'no');
        }
    }
}

function display_contributors($content) {

    //Code to be inserted here.
    return $content;
}

add_filter('the_content','display_contributors');

?>

0 个答案:

没有答案