2012-02-23 33 views
5

nTôi đã tạo một tiện ích cho người dùng để chọn hình ảnh hiển thị trong thanh bên liên kết đến trang mà họ chọn. Tôi muốn trang đó được chọn qua wp_dropdown_pages.wp_dropdown_pages làm tùy chọn trong tiện ích của tôi

Tôi rất giỏi về việc có người dùng đặt url hình ảnh, chiều cao và chiều rộng, nhưng lưu mục tiêu là nơi tôi bị kẹt.

class detc_cta_widget extends WP_Widget { 

// Constructor // 

function detc_cta_widget() { 
    $widget_ops = array('classname' => 'detc_cta_widget', 'description' => 'Displays a Phone CTA'); // Widget Settings 
    $control_ops = array('id_base' => 'detc_cta_widget'); // Widget Control Settings 
    $this->WP_Widget('detc_cta_widget', 'DETC - CTA', $widget_ops, $control_ops); // Create the widget 
} 

// Extract Args // 

    function widget($args, $instance) { 
     extract($args); 
     $title = apply_filters('widget_title', $instance['title']); // the widget title 
     $img_source  = $instance['img_source']; // CTA Image Source URL 
     $img_height  = $instance['img_height']; // CTA Image Height 
     $img_width = $instance['img_width']; // CTA Image Width 
     $link_target = $instance['link_target']; // CTA Link Target 

// Before widget // 

     echo $before_widget; 

// Title of widget // 

     if ($title) { echo $before_title . $title . $after_title; } 

// Widget output // 
     ?> 
     <div> 
     <a href="<?php echo $link_target ?>"><img src="<?php echo $img_source ?>" height="<?php echo $img_height ?>" width="<?php echo $img_width ?>"></a> 
     </div> 
     <?php 

// After widget // 

     echo $after_widget; 

    } 

// Update Settings // 

    function update($new_instance, $old_instance) { 
     $instance['title'] = strip_tags($new_instance['title']); 
     $instance['img_source'] = strip_tags($new_instance['img_source']); 
     $instance['img_height'] = strip_tags($new_instance['img_height']); 
     $instance['img_width'] = strip_tags($new_instance['img_width']); 
     $instance['link_target'] = strip_tags($new_instance['link_target']); 
     return $instance; 
    } 

// Widget Control Panel // 

    function form($instance) { 

    $defaults = array('title' => '','img_source' => 'wp-content/themes/corvius/images/cta_img.png','img_height' => '50','img_width' => '200','link_target' => 'cta.php'); 
    $instance = wp_parse_args((array) $instance, $defaults); ?> 

    <p> 
     <label for="<?php echo $this->get_field_id('title'); ?>">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 $instance['title']; ?>" /> 
    </p> 
    <p> 
     <label for="<?php echo $this->get_field_id('img_source'); ?>"><?php _e('CTA Image URL:'); ?></label> 
     <input class="widefat" id="<?php echo $this->get_field_id('img_source'); ?>" name="<?php echo $this->get_field_name('img_source'); ?>" type="text" value="<?php echo $instance['img_source']; ?>" /> 
    </p> 
    <p> 
     <label for="<?php echo $this->get_field_id('img_height'); ?>"><?php _e('CTA Image Height:'); ?></label> 
     <input class="widefat" id="<?php echo $this->get_field_id('img_height'); ?>" name="<?php echo $this->get_field_name('img_height'); ?>" type="text" value="<?php echo $instance['img_height']; ?>" /> 
    </p> 
    <p> 
     <label for="<?php echo $this->get_field_id('img_width'); ?>"><?php _e('CTA Image Width:'); ?></label> 
     <input class="widefat" id="<?php echo $this->get_field_id('img_width'); ?>" name="<?php echo $this->get_field_name('img_width'); ?>" type="text" value="<?php echo $instance['img_width']; ?>" /> 
    </p> 
    <p> 
     <label for="<?php echo $this->get_field_id('link_target'); ?>"><?php _e('CTA Link Target:'); ?></label> 
     <?php wp_dropdown_pages(); ?> 
    </p> 



    <?php } 


} 
// End class detc_cta_widget 

add_action('widgets_init', create_function('', 'return register_widget("detc_cta_widget");')); 
?> 

Đây là những gì khu vực Widget Tùy chọn trông giống như:

enter image description here

UPDATE: @Simon Tôi đã thay đổi mã Control Panel để:

<p> 
     <label for="<?php echo $this->get_field_id('link_target'); ?>"><?php _e('CTA Link Target:'); ?></label> 
     <?php wp_dropdown_pages(array('id' => $this->get_field_id('link_target'),'name' => $this->get_field_name('link_target'))); ?> 
    </p> 

Nhưng vẫn không có thành công trong việc lựa chọn được lưu.

Trả lời

8

Bạn cần cung cấp wp_dropdown_pages một thuộc tính tên thích hợp (và id sẽ đẹp cho phần tử label) của bạn. Điều này nên làm:

wp_dropdown_pages(array(
    'id' => $this->get_field_id('link_target'), 
    'name' => $this->get_field_name('link_target'), 
    'selected' => $instance['link_target'], 
); 
+0

Tôi đã cập nhật câu hỏi, có thể tôi không hiểu câu trả lời của bạn? –

+0

Ah, tôi đã bỏ lỡ phím 'select'. Trên thực tế lựa chọn của bạn đã được lưu, chỉ cần không được hiển thị. Đã cập nhật câu trả lời! – Simon

+0

Công việc tuyệt vời, "đã chọn" trả về id trang, không có vấn đề gì ngay cả khi tôi đã tổ chức các liên kết cố định của tôi để sử dụng tiêu đề trang! Đối với những người muốn theo dõi, hãy đảm bảo bạn đặt anchor href thành "? Page_id = " –

0

Tôi tin rằng mã này sẽ hoạt động tốt hơn.

wp_dropdown_pages(array( 'id' => $this->get_field_id('link_target'), 'name' => $this->get_field_name('link_target'), 'selected' => $link_target, ));

Các vấn đề liên quan