如何在Wordpress帖子上通过其短代码添加小部件

时间:2013-11-28 09:41:03

标签: wordpress

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

2 个答案:

答案 0 :(得分:0)

我完全不明白你的问题。但据我所知,可能是以下解决方案可以帮助您:

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

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

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

希望它可以帮助你...... !!!

答案 1 :(得分:0)

将此功能添加到主题

的functions.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"]