Adding Widgets Between Posts in the Index Page

For a child theme of the (current) default theme Twenty Fifteen – a way to add a new sidebar aka widget area to show after the first post in the index page; making use of the action hook ‘the_post’.

/* WIDGET AREAS aka SIDEBARS INBETWEEN POSTS IN LOOP OF INDEX PAGE */

add_action( 'after_setup_theme', 'twentyfifteen_child_setup' );

function twentyfifteen_child_setup() {
add_action( 'widgets_init', 'twentyfifteen_child_widgets_init' );
add_action( 'the_post', 'twentyfifteen_child_inter_posts_sidebars' );
}

//register sidebar for inbetween posts

function twentyfifteen_child_widgets_init() {

	register_sidebar( array(
		'name' => __( 'First Inter-Posts Widget Area ', 'twentyfifteen' ),
		'id' => 'inter-posts-1',
		'description'   => __( 'Add widgets here to appear between post 1 and 2 in your index page.', 'twentyfifteen' ),
		'before_widget' => '<aside id="%1$s" class="widget inter-post-widget %2$s">',
		'after_widget'  => '</aside>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

}

//widget output between posts in index page

function twentyfifteen_child_inter_posts_sidebars() {
	global $wp_query;
	if( is_home() && $wp_query->current_post == 1 ) {
		dynamic_sidebar( 'inter-posts-1' );
	}
	
}

you will need to create your own CSS based on the CSS class .inter-post-widget …

example:

.inter-post-widget { 
	margin: 20px 8.33%;
	padding: 3%;
	width: auto;
}

A Sidebar Template for a Page in the ‘One-Column’ Layout in Twenty Eleven

This article describes the necessary coding to create a page template with a sidebar within a ‘one-column’ layout of Twenty Eleven; the example is for a right sidebar.

All changes have to be made in a child theme of Twenty Eleven.

Create your own page template from a copy of the sidebar-page.php template; call it sidebar-page-right-in-one-column.php; edit the Template Name and description.

<?php
/**
* Template Name: Sidebar right in Page in 'One-Column'
* Description: A Page Template that adds a right sidebar to a static page in the 'one-column' theme option
*
* @package WordPress
* @subpackage Twenty_Eleven
* @since Twenty Eleven 1.0
*/

get_header(); ?>

<div id="primary">
<div id="content" role="main">

<?php while ( have_posts() ) : the_post(); ?>

<?php get_template_part( 'content', 'page' ); ?>

<?php comments_template( '', true ); ?>

<?php endwhile; // end of the loop. ?>

</div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Then make a few edits to sidebar.php and functions.php, to include the new page template into the coding;

start by editing sidebar.php to look like ( http://pastebin.com/pdqsRY8x ):

<?php
/**
* The Sidebar containing the main widget area.
*
* @package WordPress
* @subpackage Twenty_Eleven
* @since Twenty Eleven 1.0
*/

$options = twentyeleven_get_theme_options();
$current_layout = $options['theme_layout'];

if ( 'content' != $current_layout || is_page_template('sidebar-page-right-in-one-column.php') ) :
?>
<div id="secondary" role="complementary">
<?php if ( ! dynamic_sidebar( 'sidebar-1' ) ) : ?>

<aside id="archives">
<h3><?php _e( 'Archives', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_get_archives( array( 'type' => 'monthly' ) ); ?>
</ul>
</aside>

<aside id="meta">
<h3><?php _e( 'Meta', 'twentyeleven' ); ?></h3>
<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<?php wp_meta(); ?>
</ul>
</aside>

<?php endif; // end sidebar widget area ?>
</div><!-- #secondary .widget-area -->
<?php endif; ?>

Then add this section to functions.php to readjust the body_classes ( http://pastebin.com/nubXyyty ):

function twentyeleven_child_pagetemplates_body_classes( $wp_classes, $extra ){

$classes = array();

if( is_page_template( 'sidebar-page-right-in-one-column.php' ) ) :
// correction for the Sidebar Template 'right sidebar on page in one-column layout'
$classes[] = 'right-sidebar';
$classes[] = 'two-column';
$blacklist = array('one-column','content','left-sidebar','singular');
// Filter the body classes
foreach( $blacklist as $val ) {
if (!in_array($val, $wp_classes)) : continue;
else:
foreach($wp_classes as $key => $value) {
if ($value == $val) unset($wp_classes[$key]);
}
endif;
}
endif;

return array_merge($wp_classes, (array) $extra, $classes );
}

add_filter( 'body_class', 'twentyeleven_child_pagetemplates_body_classes', 20, 2 );

Last thing is to set the max width for the .page-template-sidebar-page-right-in-one-column-php #page to 690px, to keep the narrower width for the new page template.

Adding a Custom Widget Area to a WordPress Template

The Codex has a chapter on how to add new widget areas (commonly referred to as ‘sidebars’) to a theme’s template: ‘Widgetizing Themes

This example here is to give you the direct code to add to functions.php and to the template of your theme.

First step: register the custom sidebar

– this goes into functions.php:

register_sidebar(array(
'name'         => 'Custom Widget Area',
'id'            => 'custom-widgets',
'description'   => 'a custom widget area',
'before_widget' => '<li id="%1$s">',
'after_widget'  => '</li>',
'before_title'  => '<h2>',
'after_title'   => '</h2>'    ));

Second step: the custom div with the dynamic ‘sidebar’

– this goes into the template at the location where you want the custom widget to appear:

<?php if ( is_active_sidebar( 'custom-widgets' ) ) : ?>
<div class="custom"><ul>
<?php if ( !dynamic_sidebar( 'custom-widgets' ) ) : ?>
<?php endif; ?>
</ul></div>
<?php endif; ?>

The outer conditional statement makes sure that the custom div with the widget is only shown if there is actually an active widget in the custom area.

The most basic code to output the widget would be:

<?php dynamic_sidebar( 'custom-widgets' ); ?>

The parameter for registering the widget are taken from the example in the codex; you need to adapt them to fit the existing structure of your theme.
You will of course need to add some styles to format the new custom widget area according to your ideas;
for instance to style the title and to suppress the bullets:

.custom .widgettitle { font-family: arial, sans-serif; }
.custom ul { list-style-type: none; }