1

i want to display posts with custom style by custom widget in theme this is my custom widget file

    <?php
class box1 extends WP_Widget {
    function __construct() {
        parent::__construct(
            'yiw_pro_widget',
            __('ابزارک اختصاصی باکس مطالب مدل اول', 'wordpress'),
            array( 'description' => __( 'ابزارک نمایش مطالب دسته ای خاص با استایل متفاوت مدل اول', 'wordpress' ), )
        );
    }
    public function widget( $args, $instance ) {
        $name = apply_filters( 'widget_title', $instance['name'] );
        $link = apply_filters( 'widget_title', $instance['link'] );
        $cat = apply_filters( 'widget_title', $instance['cat'] );
        $color = apply_filters( 'widget_title', $instance['color'] );
?>      
        <section id="box" class="box1">
                        <h5 style="border-bottom:2px solid <?php echo $color ?>;"><a style="background:<?php echo $color ?>;" href="<?php echo $link ?>"><?php echo $name ?></a></h5>
                        <div class="col-md-6 col-xs-12">
                        <?php
                                        $portfolio = new WP_Query(array(
                                        'post_status' =>'publish',
                                        'post_type' =>'post',
                                        'cat' =>$cat,
                                        'posts_per_page' =>'1'      
                                        ));
                            if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?>
                            <article class="first">
                                <figure><?php the_post_thumbnail('box1'); ?></figure>
                                <h2><a href="<?php the_permalink() ?>"><?php the_title();?></a></h2>
                                <ul><li><i class="glyphicon glyphicon-calendar"></i><a><?php the_time('d M Y');?></a></li><li><i class="glyphicon glyphicon-comment"></i><a><?php comments_number();?></a></li></ul>
                                <p><?php the_excerpt(); ?></p>
                            </article>
                            <?php endwhile; endif;  wp_reset_query(); ?>
                        </div>
                        <div class="col-md-6 col-xs-12">
                        <?php
                                        $portfolio = new WP_Query(array(
                                        'post_status' =>'publish',
                                        'post_type' =>'post',
                                        'cat' =>$cat,
                                        'offset' => '1'
                                        'posts_per_page' =>'4'      
                                        ));
                            if($portfolio->have_posts()) : while($portfolio->have_posts()) : $portfolio->the_post(); ?>
                            <article class="first">
                                <figure><?php the_post_thumbnail('box12'); ?></figure>
                                <h2><a href="<?php the_permalink()?>"><?php the_title(); ?></a></h2>
                                <ul><li><i class="glyphicon glyphicon-calendar"></i><a><?php the_time('d M Y');?></a></li><li><i class="glyphicon glyphicon-comment"></i><a><?php comments_number(); ?></a></li></ul>
                            </article>
                            <?php endwhile; endif;  wp_reset_query(); ?>
                        </div>
                        <div class="clear"></div>
                    </section>
                    <?php
        echo $args['after_widget'];
    }
    public function form( $instance ) {
        $name   = ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
        $link   = ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
        $color  = ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
        $cat    = ( isset( $instance[ 'cat' ] ) ) ? $instance[ 'cat' ] : '';
        ?>

            <p>
                <label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان دسته بندی:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو دسته بندی:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
            </p>
            <p>
                <label for="<?php echo $this->get_field_id( 'cat' ); ?>"><?php _e( 'آی دی دسته بندی:' ); ?></label>
                <input class="widefat" id="<?php echo $this->get_field_id( 'cat' ); ?>" name="<?php echo $this->get_field_name( 'cat' ); ?>" type="text" value="<?php echo esc_attr( $cat ); ?>" placeholder="پس از نصب افزونه wp_show_id از بخش نوشته ها/دسته ها آی دی دسته بندی مورد نظر را وارد کنید" />
            </p>

        <?php
    }
    public function update( $new_instance, $old_instance ) {
        $instance = array();

        $instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
        $instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
        $instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
        $instance['cat'] = ( ! empty( $new_instance['cat'] ) ) ? strip_tags( $new_instance['cat'] ) : '';
        return $instance;
    }
}
function load_box1() {
    register_widget( 'box1' );
}
add_action( 'widgets_init', 'load_box1' );
?>

there is error

Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in C:\xampp\htdocs\wordpress\wp-content\themes\aftab\box1.php on line 42
1

on line 41 you forget the "," at the end

'offset' => '1',

  • thx this problem solved but there is no custom widget in appearance/widgets!!!! – m.javad Koushki Oct 9 '16 at 11:35
  • this is something else, create a new question – mmm Oct 9 '16 at 11:37

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