Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php wp_下拉_页面作为“我的小部件”中的选项_Php_Html_Wordpress_Widget - Fatal编程技术网

Php wp_下拉_页面作为“我的小部件”中的选项

Php wp_下拉_页面作为“我的小部件”中的选项,php,html,wordpress,widget,Php,Html,Wordpress,Widget,我已经创建了一个小部件,用户可以选择一个图像显示在侧栏中,该侧栏链接到他们选择的页面。我想通过wp_下拉页面选择该页面 我很擅长让用户设置图像url、高度和宽度,但保存目标是我遇到的难题 class detc_cta_widget extends WP_Widget { // Constructor // function detc_cta_widget() { $widget_ops = array( 'classname' => 'detc_cta_widget', 'de

我已经创建了一个小部件,用户可以选择一个图像显示在侧栏中,该侧栏链接到他们选择的页面。我想通过wp_下拉页面选择该页面

我很擅长让用户设置图像url、高度和宽度,但保存目标是我遇到的难题

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");'));
?>
class-detc\u-cta\u小部件扩展了WP\u小部件{
//建造师//
函数detc_cta_widget(){
$widget_ops=array('classname'=>'detc_cta_widget','description'=>'显示电话cta');//小部件设置
$control\u ops=array('id\u base'=>'detc\u cta\u widget');//widget控件设置
$this->WP_小部件('detc_cta_小部件','detc-cta',$Widget_ops,$control_ops);//创建小部件
}
//提取Args//
函数小部件($args$instance){
摘录($args);
$title=apply_filters('widget_title',$instance['title']);//小部件标题
$img_source=$instance['img_source'];//CTA图像源URL
$img_height=$instance['img_height'];//CTA图像高度
$img_width=$instance['img_width'];//CTA图像宽度
$link\u target=$instance['link\u target'];//CTA链接目标
//小部件之前//
echo$before_小部件;
//小部件的标题//
if($title){echo$before_title.$title.$before_title;}
//小部件输出//
?>


您需要为
wp\u下拉列表\u页面
提供一个正确的名称属性(id对于
标签
元素来说很好)。这应该可以:

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

我相信这个代码会更好

wp_下拉列表_页面(数组)(
'id'=>this->get_field_id('link_target'),
'name'=>this->get_field_name('link_target'),
'选定'=>$link\u目标,

))

我已经更新了问题,也许我不明白你的答案?啊,我错过了
选择
键。实际上你的选择被保存了,只是没有显示。答案更新了!做得好,“选择”返回页面id,即使我更改了永久链接以使用页面标题,这也不是问题!对于想跟随的用户,请确保将您的锚href设置为“?page\u id=“更好:使用
永久链接($link\u target)
,生成的URL将反映您的永久链接设置。
wp_dropdown_pages(array(
    'id' => $this->get_field_id('link_target'),
    'name' => $this->get_field_name('link_target'),
    'selected' => $instance['link_target'],
);