1

我想为我的小部件创建一个简码。

这是我创建小部件的代码,如果我想为这个小部件创建短代码,我该怎么做?

<pre>
function my_slider_post() {
    register_widget('my_slider_post');
    }
    add_action('widgets_init','my_slider_post');




class my_slider_post extends WP_Widget {
    function my_slider_post() {

        $widget_ops = array('classname' => 'my_slider_post','description' => __('Widget display Slider Post','theme'));
         add_action('admin_enqueue_scripts', array($this, 'scripts'));
        parent::__construct('mysliderpost',__('my Slider Post','theme'),$widget_ops);

        }

    function widget( $args, $instance ) {
        extract( $args );
        /* User-selected settings. */
    $title = apply_filters('widget_title', $instance['title'] );
    $img1 = $instance['image_one'];
    $title1 = $instance['title_one'];
    $desc1 = $instance['desc_one'];
    $img2 = $instance['image_two'];
    $img3 = $instance['image_three'];

        /* Before widget (defined by themes). */
        echo $before_widget;

        /* Title of widget (before and after defined by themes). */
        if ( $title )
            echo $before_title . $title . $after_title;



<!-- start -->

// code html to show





        /* After widget (defined by themes). */
        echo $after_widget;

    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;

        /* Strip tags (if needed) and update the widget settings. */
            $instance['title'] = strip_tags( $new_instance['title'] );
            $instance['image_one'] = $new_instance['image_one'];
            $instance['title_one'] = $new_instance['title_one'];
            $instance['desc_one'] = $new_instance['desc_one'];
            $instance['image_two'] = $new_instance['image_two'];
            $instance['image_three'] = $new_instance['image_three'];

        return $instance;
    }
    public function scripts()
    {
       wp_enqueue_script( 'media-upload' );
       wp_enqueue_media();
       wp_enqueue_script('our_admin', get_template_directory_uri() . '/js/our_admin.js', array('jquery'));
    }
    public function form( $instance ) {
    // code html to show on widget into sidebar control panel 

     }



    }



</pre>
4

2 回答 2

1

对于您的小部件类“my_slider_post”,您可以使用以下代码:

<?php add_shortcode( 'show_slider', 'slider_shortcode' );
function slider_shortcode( $atts ) {
    // Configure defaults and extract the attributes into variables
    extract( shortcode_atts(
        array( 
            'type'   => 'my_slider_post '
            //you can add here more parameters below
        ),
        $atts
    ));

    $args = array( //optional markup:
        'before_widget' => '<div class="box widget scheme-' . $scheme . ' ">',
        'after_widget'  => '</div>',
        'before_title'  => '<div class="widget-title">',
        'after_title'   => '</div>',
    );

    ob_start();
    the_widget( $type, $atts, $args );
    $output = ob_get_clean();
    return $output;
}

然后使用此代码

[show_slider]

运行小部件。

我使用了这篇文章:https ://www.smashingmagazine.com/2012/12/inserting-widgets-with-shortcodes/

于 2021-05-05T18:38:07.173 回答
0

添加

add_shortcode('the_slider_short_code', 'my_slider_post');在您的帖子、页面或自定义帖子类型中class my_slider_post extends WP_Widget调用简码。[the_slider_short_code]

或者,您可以使用amr 简码插件为任何小部件生成简码。你可以从这里阅读更多关于它的信息

让我知道事情的后续。

于 2018-02-10T05:09:16.563 回答