[theme-reviewers] Setting $content_width Dynamically
Chip Bennett
chip at chipbennett.net
Wed Aug 10 15:11:34 UTC 2011
Good morning, reviewers and developers!
I've added page layout options to Oenology, and since the layouts include
variable widths for my #content area, I've been experimenting with ways to
set $content_width dynamically. I thought I'd share my (working!) code.
Basically, I pulled the $content_width code out of the Theme setup function,
which is hooked into the 'after_setup_theme' action hook, and instead put it
in its own function, hooked into the 'wp_head' action hook. This change
allows me to use query conditionals and the $post global.
Here's what I'm using:
/**
* Dynamically set $content_width
*
* Define $content_width global variable, to keep
* media content from overflowing the Theme's
* main content area.
*/
function oenology_set_content_width() {
global $oenology_options;
$oenology_options = oenology_get_options();
// Set variables for each layout/context
$width_three_column = 635;
$width_two_column = 810;
$width_one_column = 815;
$width_attachment = 888;
global $post;
$layout = oenology_get_current_page_layout();
// Set default content width
//
// The default layout is the three-column
// layout for static Pages, and the two-
// column layout for single posts and post
// indexes.
//
// Note: the width of the *content* area,
// which is div#main, is the same for the
// three-column static Page layout as for
// the two-column post/index layout.
$dynamic_width = $width_three_column;
// Set content width for attachment pages
if ( is_attachment() ) {
$dynamic_width = $width_attachment;
}
// Set content width for one-column layout
else if ( 'one-column' == $layout ) {
$dynamic_width = $width_one_column;
}
// Set content width for two-column layout
// Note: only applies to static Pages
else if ( 'two-column' == $layout ) {
$dynamic_width = $width_two_column;
}
// Apply dynamic width to $content_width
global $content_width;
if ( ! isset( $content_width ) ) {
$content_width = $dynamic_width;
}
}
add_action( 'wp_head', 'oenology_set_content_width' );
Note that oenology_get_current_page_layout() is used to return the actual
layout. It is the function that needs access to the $post global (for custom
post meta data), as well as query conditionals such as is_single(),
is_page(), is_attachment(), etc.:
/**
* Get Current Page Layout
*/
function oenology_get_current_page_layout() {
global $post, $oenology_options;
$custom = ( get_post_custom( $post->ID ) ? get_post_custom( $post->ID ) :
false );
$custom_layout = ( isset( $custom['_oenology_layout'][0] ) ?
$custom['_oenology_layout'][0] : 'default' );
$layout = '';
if ( is_page() ) {
if ( 'default' == $custom_layout ) {
$layout .= $oenology_options['default_static_page_layout'];
} else {
$layout .= $custom_layout;
}
} else if ( is_single() ) {
if ( 'default' == $custom_layout ) {
$layout .= $oenology_options['default_single_post_layout'];
} else {
$layout .= $custom_layout;
}
} else if ( is_home() || is_archive() || is_search() || is_404() ) {
$layout .= $oenology_options['post_index_layout'];
}
return $layout;
}
Perhaps some will find this useful, I hope!
Chip
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.wordpress.org/pipermail/theme-reviewers/attachments/20110810/c81de93a/attachment.htm>
More information about the theme-reviewers
mailing list