0

I create a widget for a custom post type that will display a list of post in the widget area. it works fine, i can select multiple posts and save the widget but i can not get it to display on the frontend. UPDATE so basically what i'm trying to achieve is list all the posts in the widget area with a checkbox (works fine), when a checkbox is checked it will show that post in the frontend, if checkbox isn't checked it will not show on the frontend. that s the part that isn't working, i get no coNtent output on the frontend 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
  • Widget doesn't display at all or you can't get it to display how you like it? – Z. Zlatev Apr 24 '16 at 7:29
  • @Zlatev the widget shows up in the frontend but the content doesnt show, so basically what im trying to achieve is list all the posts in the widget area with a checkbox (works fine), when a checkbox is checked it will show that post in the frontend, if checkbox isn't checked it will not show on the frontend. that s the part that isnt working, i get no cotent output on the frontend. – frenchy black Apr 24 '16 at 18:05
1

Without actually knowing what you're trying to achieve I see the following problems:

  • $post variable is not defined in your widget() method. Try setting it using $post = get_post( get_the_ID() ) for instance.
  • By the time you call foreach ($posts as $post) { ... } $posts variable is also not set. You need some query for this one I guess - WP_Query() or get_posts()

In order to effectively catch such problems set WP_DEBUG and WP_DEBUG_LOG constants both to true in your wp-config.php file then you'll be able to monitor wp-content/debug.log for notices and errors. Good luck.

1
  • Zlatev thank you, basically what im trying to achieve is list all the posts in the widget area with a checkbox, when a checkbox is checked it will show that post in the frontend, if checkbox isn't checked it will not show on the frontend. – frenchy black Apr 24 '16 at 17:59
0

Here is what you are looking for:

<?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');
1
  • Hi @sacgro, welcome to WPSE! Please feel free to take the tour. Would you mind adding some explanation to your post of what your code does? – Tim Malone May 4 '16 at 21:58

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.