在任何地方显示WordPress小工具

WPOCN WordPress教程评论77阅读模式

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

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

// 在正文最后添加最近文章小工具
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;
}

在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
我的微信
我的微信
微信扫一扫
 
WPOCN
  • 本文由 WPOCN 发表于 2022年11月21日15:07:40
  • 转载请务必保留本文链接:https://www.tghello.com/4309.html

发表评论