在前端显示小部件复选框选择

时间:2016-04-24 作者:frenchy black

我为自定义帖子类型创建了一个小部件,它将在小部件区域中显示帖子列表。它工作正常,我可以选择多个帖子并保存小部件,但我无法让它显示在前端。UPDATE因此,基本上,我试图实现的是用一个复选框列出小部件区域中的所有帖子(效果很好),当选中复选框时,它将在前端显示该帖子,如果不选中复选框,它将不会显示在前端。这是不起作用的部分,我在前端没有内容输出HERE IS MY CODE

    <?php class mm_location_widget extends WP_Widget {
    function __construct() {
    parent::__construct(
    // Base ID of your widget
    \'mm_location_widget\', 

    // Widget name will appear in UI
    __(\'Locations By Marvil Media Widget\', \'mm_location_widget_domain\'), 

    // Widget description
    array( \'description\' => __( \'Locations Widget By Marvil Media\', \'mm_location_widget_domain\' ), ) 
    );
    }

    // Creating widget front-end
    // This is where the action happens
    public function widget( $args, $instance ) {
    $title = apply_filters( \'widget_title\', $instance[\'title\'] );
    $your_checkbox_var = $instance[ \'your_checkbox_var\' ] ? \'true\' : \'false\';
    // before and after widget arguments are defined by themes
    echo $args[\'before_widget\'];
    if ( ! empty( $title ) )
    echo $args[\'before_title\'] . $title . $args[\'after_title\'];

    // This is where you run the code and display the output
    //echo __( \'Hello, World!\', \'mm_location_widget_domain\' );

    // Retrieve the checkbox

    if ( $your_checkbox_var == \'checked\' ) {
    $loc_id = $post->ID;
    $loc_title = get_the_title($post->ID);
    $loc_link = get_permalink($post->ID);
    $loc_address = get_post_meta( get_the_ID(), \'_marvilmedia_address\', true );
    $loc_city = get_post_meta( get_the_ID(), \'_marvilmedia_city\', true );
    $loc_state = get_post_meta( get_the_ID(), \'_marvilmedia_state\', true );
    $loc_zipcode = get_post_meta( get_the_ID(), \'_marvilmedia_zipcode\', true );
    $loc_phone = get_post_meta( get_the_ID(), \'_marvilmedia_phone\', true );
    $loc_direction = get_post_meta( get_the_ID(), \'_marvilmedia_direction\', true );


    // echo "<ul>";
    foreach ($posts as $post) {
    $loc_info .= \'<div class="col-xs-12">\';
    $loc_info .= \'<h5><a target="_blank" href="\'.get_the_permalink().\'">\' . get_the_title() . \'</a></h5>\';
    $loc_info .= \'<p>\';
    $loc_info .= $loc_address.\'<br/>\';
    $loc_info .= $loc_city.\',&nbsp;\'.$loc_state.\'&nbsp;\'.$loc_zipcode.\'\';   
    $loc_info .= $loc_phone.\'<br/>\';
    $loc_info .= \'</p>\';  
    $loc_info .= \'</div>\';
    echo $loc_info;
    }
    // echo "</ul>";
    }; 
    ?>

    <?php // Retrieve the checkbox
    if( \'checked\' == $instance[ \'your_checkbox_var\' ] ) : 
    $loc_id = $post->ID;
    $loc_title = get_the_title($post->ID);
    $loc_link = get_permalink($post->ID);
    $loc_address = get_post_meta( get_the_ID(), \'_marvilmedia_address\', true );
    $loc_city = get_post_meta( get_the_ID(), \'_marvilmedia_city\', true );
    $loc_state = get_post_meta( get_the_ID(), \'_marvilmedia_state\', true );
    $loc_zipcode = get_post_meta( get_the_ID(), \'_marvilmedia_zipcode\', true );
    $loc_phone = get_post_meta( get_the_ID(), \'_marvilmedia_phone\', true );
    $loc_direction = get_post_meta( get_the_ID(), \'_marvilmedia_direction\', true );


    // echo "<ul>";
    foreach ($posts as $post) {
    $loc_info .= \'<div class="col-xs-12">\';
    $loc_info .= \'<h5><a target="_blank" href="\'.get_the_permalink().\'">\' . get_the_title() . \'</a></h5>\';
    $loc_info .= \'<p>\';
    $loc_info .= $loc_address.\'<br/>\';
    $loc_info .= $loc_city.\',&nbsp;\'.$loc_state.\'&nbsp;\'.$loc_zipcode.\'\';   
    $loc_info .= $loc_phone.\'<br/>\';
    $loc_info .= \'</p>\';  
    $loc_info .= \'</div>\';
    echo $loc_info;
    }
    // echo "</ul>";
    endif; 



    echo $args[\'after_widget\'];
    }

    // Widget Backend 
    public function form( $instance ) {
    if ( isset( $instance[ \'title\' ] ) ) {
    $title = $instance[ \'title\' ];
    }
    else {
    $title = __( \'Locations\', \'mm_location_widget_domain\' );
    }
    // Widget admin form
    ?>

    <p>
    <label for="<?php echo $this->get_field_id( \'title\' ); ?>"><?php _e( \'Title:\' ); ?></label> 
    <input class="widefat" id="<?php echo $this->get_field_id( \'title\' ); ?>" name="<?php echo $this->get_field_name( \'title\' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
    </p>

    <p>
    <label for="<?php echo $this->get_field_id( \'your_checkbox_var\' ); ?>"><?php _e( \'Locations:\' ); ?></label><br/>
    <?php
    $args = array( \'post_type\' => \'mm_location\', \'posts_per_page\' => -1, \'post_status\' =>\'any\', \'post_parent\' => $post->ID );

    $myposts = get_posts( $args );
    foreach ( $myposts as $post )  {
    $option=\'<input type="checkbox" id="\'. $this->get_field_id( \'your_checkbox_var\' ) .\'[]" name="\'. $this->get_field_name( \'your_checkbox_var\' ) .\'[]"\';
    //if (is_array($instance[\'your_checkbox_var\'])) {
    foreach ($instance[\'your_checkbox_var\'] as $posts) {
    if($posts==$post->ID) {
    $option=$option.=\' checked="checked"\';
    }
    }
    //}
    $option .= \' value="\'.$post->ID.\'" />\';

    $option .= get_the_title($post->ID);
    $option .= \'<br />\';
    echo $option;
    } ?>
    </p>           


    <?php 
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance[\'title\'] = ( ! empty( $new_instance[\'title\'] ) ) ? strip_tags( $new_instance[\'title\'] ) : \'\';
    $instance[ \'your_checkbox_var\' ] = $new_instance[ \'your_checkbox_var\' ];
    return $instance;
    }
    } // Class mm_location_widget ends here

    // Register and load the widget
    function marvilmedia_loc_load_widget() {
    register_widget( \'mm_location_widget\' );
    }
    add_action( \'widgets_init\', \'marvilmedia_loc_load_widget\' );
    ?>

2 个回复
最合适的回答,由SO网友:Z. Zlatev 整理而成

在不知道您想要实现什么目标的情况下,我发现了以下问题:

  • $post 变量未在中定义widget() 方法尝试使用$post = get_post( get_the_ID() ) 例如foreach ($posts as $post) { ... } $posts 变量也未设置。我想你需要一些关于这个的查询-WP_Query()get_posts()WP_DEBUG 和WP_DEBUG_LOG 常量均为true 在您的wp-config.php 然后您可以监视wp-content/debug.log 注意事项和错误。祝你好运

SO网友:sacgro

以下是您正在寻找的内容:

<?php
class mm_location_widget extends WP_Widget
{
    function __construct()
    {
        parent::__construct(
        // Base ID of your widget
            \'mm_location_widget\', 
        // Widget name will appear in UI
            __(\'Locations By Marvil Media Widget\', \'mm_location_widget_domain\'), 
        // Widget description
            array(
            \'description\' => __(\'Locations Widget By Marvil Media\', \'mm_location_widget_domain\')
        ));
    }

    // Creating widget front-end
    // This is where the action happens
    public function widget($args, $instance)
    {
        $title = apply_filters(\'widget_title\', $instance[\'title\']);
        // $your_checkbox_var = $instance[ \'your_checkbox_var\' ] ? \'true\' : \'false\';
        // before and after widget arguments are defined by themes
        echo $args[\'before_widget\'];
        if (!empty($title))
            echo $args[\'before_title\'] . $title . $args[\'after_title\'];

        // This is where you run the code and display the output
        //echo __( \'Hello, World!\', \'mm_location_widget_domain\' );

        // Retrieve the checkbox
        $args1 = array(
            \'post_type\' => \'mm_location\',
            \'posts_per_page\' => -1,
            \'post_status\' => \'publish\'
        );

        $myposts = get_posts($args1);
        foreach ($myposts as $post) {
            $loc_info = "";
            $option   = false;
            foreach ($instance[\'checkbox\'] as $checks) {
                if ($checks == $post->ID) {
                    $option = true;
                }
            }
            if ($option) {
                $loc_id        = $post->ID;
                $loc_title     = get_the_title($post->ID);
                $loc_link      = get_permalink($post->ID);
                $loc_address   = get_post_meta($loc_id, \'_marvilmedia_address\', true);
                $loc_city      = get_post_meta($loc_id, \'_marvilmedia_city\', true);
                $loc_state     = get_post_meta($loc_id, \'_marvilmedia_state\', true);
                $loc_zipcode   = get_post_meta($loc_id, \'_marvilmedia_zipcode\', true);
                $loc_phone     = get_post_meta($loc_id, \'_marvilmedia_phone\', true);
                $loc_direction = get_post_meta($loc_id, \'_marvilmedia_direction\', true);

                $loc_info .= \'<div class="col-xs-12">\';
                $loc_info .= \'<h5><a target="_blank" href="\' . $loc_link . \'">\' . $loc_title . \'</a></h5>\';
                $loc_info .= \'<p>\';
                $loc_info .= $loc_address . \'<br/>\';
                $loc_info .= $loc_city . \',&nbsp;\' . $loc_state . \'&nbsp;\' . $loc_zipcode . \'\';
                $loc_info .= $loc_phone . \'<br/>\';
                $loc_info .= \'</p>\';
                $loc_info .= \'</div>\';
                echo $loc_info;

                // echo "</ul>";
            }
            ;
        }

        echo $args[\'after_widget\'];
    }

    // Widget Backend 
    public function form($instance)
    {
        if (isset($instance[\'title\'])) {
            $title = $instance[\'title\'];
        } else {
            $title = __(\'Locations\', \'mm_location_widget_domain\');
        }
        // Widget admin form
        ?>

            <p>
            <label for="<?php
                echo $this->get_field_id(\'title\');
        ?>"><?php
                _e(\'Title:\');
        ?></label> 
            <input class="widefat" id="<?php
                echo $this->get_field_id(\'title\');
        ?>" name="<?php
                echo $this->get_field_name(\'title\');
        ?>" type="text" value="<?php
                echo esc_attr($title);
        ?>" />
            </p>

        <p>
            <label for="<?php
                echo $this->get_field_id(\'checkbox\');
            ?>"><?php
                _e(\'Locations:\');
            ?></label><br/>
            <?php
            $args = array(
                \'post_type\' => \'mm_location\',
                \'posts_per_page\' => -1,
                \'post_status\' => \'publish\'
            );

            $myposts = get_posts($args);
            foreach ($myposts as $post) {
                $option    = \'<input type="checkbox" id="\' . $this->get_field_id(\'checkbox\') . \'[]" name="\' . $this->get_field_name(\'checkbox\') . \'[]"\';
                $selected  = $instance[\'checkbox\'];
                $arrlength = count($selected);

                for ($x = 0; $x < $arrlength; $x++) {
                    if ($selected[$x] == $post->ID) {
                        $option = $option .= \' checked="checked"\';
                    }
                }
                //}
                $option .= \' value="\' . $post->ID . \'" />\';

                $option .= get_the_title($post->ID);
                $option .= \'<br />\';
                echo $option;
            }
            ?>
       </p>          

        <?php
    }

    // Updating widget replacing old instances with new
    public function update($new_instance, $old_instance)
    {
        $instance             = array();
        $instance[\'title\']    = (!empty($new_instance[\'title\'])) ? strip_tags($new_instance[\'title\']) : \'\';
        $instance[\'checkbox\'] = $new_instance[\'checkbox\'];
        return $instance;
    }
} // Class mm_location_widget ends here

// Register and load the widget
function marvilmedia_loc_load_widget()
{
    register_widget(\'mm_location_widget\');
}
add_action(\'widgets_init\', \'marvilmedia_loc_load_widget\');

相关推荐

在现有WP_LIST_TABLE中添加和删除行操作

我想修改现有WP\\U List\\U表的行操作。例如,在后端的用户部分,我想删除;“编辑”;每个用户的选项。我还想添加新的操作,例如;书籍“;,这将列出该用户阅读的书籍(这与另一个插件一起使用)。我感觉这是某种过滤器,但在文档中找不到任何东西。请注意,我没有创建新表。这是一个现有的WordPress表(例如,用户)。我该怎么做?谢谢