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; }

Custom Web Site Design and Programming

I am building web sites for quite a few years now, and want to present a web site I programmed in static xhtml a while ago: kunstaufseide.com.

kunstaufseide.com
kunstaufseide.com

This is a German language site promoting colourful artwork.

Semi-integrated into this site is a kunstaufseide blog based on a WordPress.org installation with a customized theme build on the then default Kubrick theme.

With German being my mother’s tongue, creating grammatically correct bi-lingual web sites is no problem for me.

Please contact me via my email alchymyth@gmail.com if you would like me to support you with getting your own personal website up and running.

You will also find me frequently in both the German WordPress Deutschland Forum and English WordPress > Support  forums.

Adsense Ad at Random Position in a Posts Page

This code is made to output an adsense ad (or any other block of output) at a random position within the loop, with the restriction to a preset number of possible positions.

<?php $ad_positions = array(1,2,4); // array of preset possible ad positions

do { $rnd_ad = $ad_positions[rand(0,count($ad_positions)-1)]; } while ($rnd_ad >= $wp_query->post_count); 
// to make sure that the random output fits into the available number of posts ?>

<?php if(have_posts()) : while(have_posts()) : the_post(); // start of the loop // ?>

some part of the post output here

  <?php if( ($wp_query->current_post+1) == $rnd_ad ) : ?>
    adsense code here
  <?php endif; ?> 

more post output here

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

Thanks to the tutorials of http://www.php.net/ …

Numbering Posts

To give posts a continuous number in index pages or archives, try this little piece of code:

<?php
global $query_string;
  $all_post_query = new WP_Query($query_string.'&posts_per_page=-1');
    $count_posts = $all_post_query->post_count;
  $this_post_nr = 1 + $wp_query->current_post + (get_query_var('paged')?(get_query_var('paged')-1) : 0) * get_option('posts_per_page');
echo $this_post_nr.' of '.$count_posts.' in this selection of posts.';
?>

Place it within the loop;
condition: needs to be adjusted for any custom query.