2012-02-23 3 views
5

में एक विकल्प के रूप में मैंने उपयोगकर्ताओं को अपनी पसंद के एक पृष्ठ से लिंक करने वाली साइड बार में प्रदर्शित करने के लिए एक छवि चुनने के लिए एक विजेट बनाया है। मैं चाहता हूं कि वह पृष्ठ wp_dropdown_pages के माध्यम से चुना जाए।wp_dropdown_pages मेरे विजेट

मैं उपयोगकर्ताओं को छवि यूआरएल, ऊंचाई और चौड़ाई सेट करने के हिस्से में अच्छा हूं, लेकिन लक्ष्य को बचाने के लिए जहां मैं अटक गया हूं।

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");')); 
?> 

यह वही विजेट विकल्प क्षेत्र लग रहा है जैसे:

enter image description here

अद्यतन: @Simon मैं करने के लिए नियंत्रण कक्ष कोड बदल दिया है:

<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> 

लेकिन चयन में अभी भी कोई सफलता बचाई नहीं जा रही है।

उत्तर

8

आपको wp_dropdown_pages उचित नाम विशेषता देना है (और आईडी आपके label तत्व के लिए अच्छा होगा)। यह करना चाहिए:

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

मैं सवाल को नवीनीकृत किया है, शायद मैं अपने जवाब समझ में नहीं आया? –

+0

आह, मुझे 'चयन' कुंजी याद आई। वास्तव में आपका चयन सहेजा जा रहा था, बस प्रदर्शित नहीं किया जा रहा था। उत्तर अपडेट किया गया! – Simon

+0

ग्रेट जॉब, "चयनित" पृष्ठ आईडी देता है, जो कि कोई समस्या नहीं थी, यहां तक ​​कि मैंने पेज के शीर्षक का उपयोग करने के लिए मेरे परमालिंक का पीछा किया है! जो लोग अनुसरण करना चाहते हैं, उनके लिए सुनिश्चित करें कि आप अपना एंकर href "? Page_id = " –

0

मेरा मानना ​​है कि यह कोड बेहतर काम करेगा।

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

संबंधित मुद्दे