如何在WordPress中为窗口小部件创建短代码?

时间:2018-02-09 23:35:52

标签: wordpress widget shortcode wordpress-shortcode

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

这是我创建窗口小部件的代码,如果我想为此窗口小部件创建一个短代码,我该怎么办?

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

2 个答案:

答案 0 :(得分: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/

答案 1 :(得分:0)

添加

Sub SuspenseReport() Dim allColumns As Range Dim cell As Range Dim col As Range Dim x As Integer Dim i As Integer Application.ScreenUpdating = False Set allColumns = Columns("C:E") allColumns.Hidden = True Set allColumns = Columns("BN:DY") allColumns.Hidden = True Set allColumns = Columns("EB:EU") allColumns.Hidden = True Dim rng1 As Range: Set rng1 = Application.Range("G2:BO8") 'maybe limit the range to just one column and range.offet at the end? For Each col In rng.Columns If cell.Value = "NULL" Then cell.EntireColumn.Hidden = False GoTo ExitIfStat Else: cell.EntireColumn.Hidden = True End If Next col ExitIfStat: Next x 'below is another variation I attempted but the for loop would iterate on cell not column 'Dim i As Integer 'i = -1 'For Each col In Range("G1:BO8") ' i = i + 1 ' If i Mod 3 = 0 Then ' If col.Value = "NULL" Then ' col.EntireColumn.Hidden = False ' Else: col.EntireColumn.Hidden = True 'col.Offset(0, -1).EntireColumn.Hidden = True 'col.Offset(0, 1).EntireColumn.Hidden = True ' End If Application.ScreenUpdating = True End Sub 之后

add_shortcode('the_slider_short_code', 'my_slider_post');,并在帖子,网页或自定义帖子类型中将短代码称为class my_slider_post extends WP_Widget

或者,您可以使用amr shortcode插件为任何小部件生成短代码。你可以read more about it from here

让我知道它是怎么回事。