在任何地方显示WordPress小工具

正常WordPress只能在已注册的区域内使用显示小工具,下面的例子可以让小工具显示在任何地方。

在任何地方显示WordPress小工具

在任何地方显示WordPress小工具

section

例子一

在正文最后添加最近文章小工具

// 在正文最后添加最近文章小工具
add_filter( 'the_content', 'dcwd_append_recent_posts_widget_to_content' );
function dcwd_append_recent_posts_widget_to_content( $content ) {
	$rpw = new WP_Widget_Recent_posts;

	$args = array(
		'before_widget' => '<div class="widget">',
		'after_widget'  => "</div\n",
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => "</h2>\n",
	);

	$instance = array(
			'title' => 'Some Recent Posts',
			'number' => 2,
			'show_date' => true,
	);
	ob_start();
	$rpw->widget( $args, $instance );
	$output = ob_get_contents();
	ob_end_clean();
	
	return $content . $output;
}
section

例子二

在wp_body_open钩子中调用 WordPress Meta 小部件。

// 在侧边栏之外wp_body_open钩子中调用 WordPress Meta 小部件。
add_action( 'wp_body_open', 'dcwd_meta_widget_at_top' );
function dcwd_meta_widget_at_top() {
	$mw = new WP_Widget_Meta;

	$args = array(
		//'before_widget' => '<li id="%1$s" class="widget %2$s">',
		//'after_widget'  => "</li>\n",
		'before_widget' => '<div class="widget">',
		'after_widget'  => "</div\n",
		'before_title'  => '<h2 class="widgettitle">',
		'after_title'   => "</h2>\n",
	);

	$instance = array(
			'title' => 'Our Meta Widget',
	);
	$mw->widget( $args, $instance );
}

本站文章大部分为原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
微信号已复制
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证