【问题标题】:How to add a widget by its shortcode on Wordpress posts如何在 Wordpress 帖子上通过其短代码添加小部件
【发布时间】:2013-11-28 09:41:02
【问题描述】:

有什么方法可以为已经创建的小部件分配一个短代码,然后再使用它 在我们的特定帖子和页面中显示该小部件而不是简单的方法 在侧边栏中显示小部件?我用谷歌搜索了这个东西,没有找到任何相关的东西。

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    我没有完全理解你的问题。但据我了解,以下解决方案可能对您有所帮助:

    这是在页面/帖子上调用小部件的插件:

    http://wordpress.org/plugins/widgets-on-pages/

    试试上面提到的插件。它可能会对你有所帮助。

    希望对你有帮助..!!!

    【讨论】:

      【解决方案2】:

      将此函数添加到您主题的 fucntions.php 中

        function widget($atts) {
        global $wp_widget_factory;    
        extract(shortcode_atts(array(
            'widget_name' => FALSE
        ), $atts));
      
        $widget_name = wp_specialchars($widget_name);
      
        if (!is_a($wp_widget_factory->widgets[$widget_name], 'WP_Widget')):
            $wp_class = 'WP_Widget_'.ucwords(strtolower($class));
      
            if (!is_a($wp_widget_factory->widgets[$wp_class], 'WP_Widget')):
                return '<p>'.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),'<strong>'.$class.'</strong>').'</p>';
            else:
                $class = $wp_class;
            endif;
        endif;
      
        ob_start();
        the_widget($widget_name, $instance, array('widget_id'=>'arbitrary-instance-'.$id,
            'before_widget' => '',
            'after_widget' => '',
            'before_title' => '',
            'after_title' => ''
        ));
        $output = ob_get_contents();
        ob_end_clean();
        return $output;
      
      }  
      add_shortcode('widget','widget'); 
      

      并像这样使用您发布的短代码

      [widget widget_name="Your_Custom_Widget"]
      

      【讨论】:

        猜你喜欢
        • 2013-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-19
        • 2018-09-11
        • 2018-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多