注册一个小部件,当the_post
第二次调用操作:
add_action( \'wp_loaded\', \'wpse_80202_register_banner_widget\' );
function wpse_80202_register_banner_widget()
{
// used on the first page of main loop only
register_sidebar(
array (
\'name\' => \'Banner front-page \',
\'id\' => \'frontpage_banner\',
\'before_widget\' => \'<div class="frontpage-banner">\',
\'after_widget\' => \'</div>\'
)
);
}
add_action( \'loop_start\', \'wpse_80202_show_banner_widget\' );
function wpse_80202_show_banner_widget()
{
static $count = 0;
if ( ! is_front_page() )
return;
if ( \'loop_start\' === current_filter() )
{
add_action( \'the_post\', __FUNCTION__ );
return;
}
$count += 1;
if ( 2 === $count )
{
dynamic_sidebar( \'frontpage_banner\' );
remove_action( \'the_post\', __FUNCTION__ );
}
}
我会使用
Text 小部件:
此代码的另一个变体如下:Add Adsense code in index.php