dongzhou1865 2015-12-20 14:02
浏览 78

在wordpress元框中显示前端的复选框

I am building a plugin that provides author names as checkboxes on the admin side. The names that have been checked should be displayed on the front-end side in Wordpress.

I have written the code for the admin side, i.e. my code provides checkboxes and its values are saved.

How should the checked values be displayed on the front end?

<?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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害
    • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
    • ¥15 java写代码遇到问题,求帮助
    • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
    • ¥15 有了解d3和topogram.js库的吗?有偿请教
    • ¥100 任意维数的K均值聚类
    • ¥15 stamps做sbas-insar,时序沉降图怎么画
    • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
    • ¥15 关于#Java#的问题,如何解决?