Hãy đăng ký thành viên để có thể dễ dàng trao đổi, giao lưu và chia sẻ về kiến thức đồ họa.
  1. Hãy đăng ký subscribe kênh Youtube mới của Việt Designer tại địa chỉ: Youtube.com/VietDesignerChannel để theo dõi các video về thiết kế đồ họa. Do trước đó kênh cũ bị Youtube quét không rõ lý do, trong thời gian chờ kháng cáo nếu các bạn thấy video trên diễn đàn bị die không xem được thì có thể vào kênh mới để tìm xem video sơ cua nhé.
    Loại bỏ thông báo

Xin các bạn trợ giúp mình cái Widget

Chủ đề thuộc danh mục 'WordPress' được đăng bởi toctem, 19/3/14.

Lượt xem: 2,002

  1. toctem Mới đăng kí

    Mình đang thử làm theme, mình có viết một cái widget để hiển thị tin tức lên Sidebar.

    Về mặt cơ bản là ok: hiển thị backend và front-end, update, edit. Mình gặp một vấn đề đó là, khi mình kéo cái Widget này sang Sidebar lần đầu tiên thì cái form nhập dữ liệu của mình nó bị mất, co cụp này, mình có dùng firebug để xem html thì không thấy có các Input của mình :((. Mình thử Refresh F5 lại thì ok, nó là một widget ngon lành.

    Nghĩa là ở đây đang gặp vấn đề lần đầu tiên kéo sang sidebar thì bị ẩn đi. vậy mong các bạ giúp mình

    Code widget mình như sau:
    PHP:
    <?php

    class widget_hotnews extends WP_Widget {

        function 
    widget_hotnews() {
            
    $widget_ops = array('classname' => 'widget_hotnews''description' => 'description widget');
            
    $control_ops = array('width' => 200'height' => 300);
            
    $this->WP_Widget('widget_hotnews''1show last hot news'$widget_ops$control_ops);
        }

    //backend
        
    function form($instance) {
            
    $default = array('title' => 'Untitled''popular_post_num' => 5);
            
    $instance wp_parse_args((array) $instance$default);
            
    $title = isset($instance['title']) ? esc_attr($instance['title']) : 1;
            
    $popular = isset($instance['popular_post_num']) ? esc_attr($instance['popular_post_num']) : 1;
            
    ?>

            <p>
                <label>
                    Title:<input id='<?php echo $this->get_field_id("title"?>' name='<?php echo $this->get_field_name("title"?>' value='<?php echo $title?>'/>
                </label>
            </p>
            <p>
                <label>
                    popular post num:<input id='<?php echo $this->get_field_id("popular_post_num"?>' name='<?php echo $this->get_field_name("popular_post_num"?>' value='<?php echo $popular?>'/>
                </label>
            </p>

            <?php
        
    }

    //fron-end
        
    function widget($args$instance) {
            
    extract($args);
           
            
    $title apply_filters('widget_title'$instance['title']);
            echo 
    $before_widget;
            echo 
    $before_title $title $after_title;
            echo 
    $after_title;
            
    $popular_post_num $instance['popular_post_num'];
          
            
    $arrpara=array("orderby=ID","showposts"=>$popular_post_num);
            
    $objQuery=new WP_Query($arrpara);
            if(
    $objQuery->have_posts())
            {
                echo 
    "<ul>";
                while (
    $objQuery->have_posts())
                {
                    
    $objQuery->the_post();?>
                      <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                    <?php
                
    }
                echo 
    "</ul>";
            }
            echo 
    $after_widget;
        }

    //save widget in backend
        
    function update($new_instance$old_instance) {
            
    $instance $old_instance;
            
    $instance['popular_post_num'] = $new_instance['popular_post_num'];
            
    $instance['title'] = $new_instance['title'];
            return 
    $instance;
        }

    }

    add_action('widgets_init'create_function('''register_widget("widget_hotnews");'));
    ?>
    Trong file functions.php mình goi như sau:

    include('functions/widget_hotnews.php');

    ...
  2. pvandung2010

    pvandung2010 Thành viên cấp 1

    bạn thử tạo tách biệt ra, đăng ký widget rồi mới tạo chức năng
    mình tạo bình thường ko bị lỗi

Ủng hộ diễn đàn