Good morning, reviewers and developers!<div><br></div><div>I&#39;ve added page layout options to Oenology, and since the layouts include variable widths for my #content area, I&#39;ve been experimenting with ways to set $content_width dynamically. I thought I&#39;d share my (working!) code.</div>
<div><br></div><div>Basically, I pulled the $content_width code out of the Theme setup function, which is hooked into the &#39;after_setup_theme&#39; action hook, and instead put it in its own function, hooked into the &#39;wp_head&#39; action hook. This change allows me to use query conditionals and the $post global.</div>
<div><br></div><div>Here&#39;s what I&#39;m using:</div><div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div>/**</div><div> * Dynamically set $content_width</div>
<div> * </div><div> * Define $content_width global variable, to keep </div><div> * media content from overflowing the Theme&#39;s</div><div> * main content area.</div><div> */</div><div>function oenology_set_content_width() {</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>global $oenology_options;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>$oenology_options = oenology_get_options();</div><div><br>
</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// Set variables for each layout/context</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>$width_three_column = 635;</div><div>
<span class="Apple-tab-span" style="white-space:pre">                </span>$width_two_column = 810;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>$width_one_column = 815;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>$width_attachment = 888;</div>
<div><br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>global $post;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>$layout = oenology_get_current_page_layout();</div><div>
<br></div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// Set default content width</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>//</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// The default layout is the three-column</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>// layout for static Pages, and the two-</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// column layout for single posts and post</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>// indexes.</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>//</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// Note: the width of the *content* area,</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>// which is div#main, is the same for the</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// three-column static Page layout as for</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>// the two-column post/index layout.</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>$dynamic_width = $width_three_column;</div><div>
<span class="Apple-tab-span" style="white-space:pre">                </span>// Set content width for attachment pages</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>if ( is_attachment() ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>$dynamic_width = $width_attachment;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>} </div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// Set content width for one-column layout</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>else if ( &#39;one-column&#39; == $layout ) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>$dynamic_width = $width_one_column;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>} </div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// Set content width for two-column layout</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>// Note: only applies to static Pages</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>else if ( &#39;two-column&#39; == $layout ) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>$dynamic_width = $width_two_column;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>// Apply dynamic width to $content_width</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>global $content_width;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>if ( ! isset( $content_width ) ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>$content_width = $dynamic_width;</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div>}</div><div>add_action( &#39;wp_head&#39;, &#39;oenology_set_content_width&#39; );</div></div></blockquote><div><br></div><div>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.:</div>
<div><br></div><blockquote class="webkit-indent-blockquote" style="margin: 0 0 0 40px; border: none; padding: 0px;"><div><div>/**</div><div> * Get Current Page Layout</div><div> */</div><div>function oenology_get_current_page_layout() {</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>global $post, $oenology_options;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>$custom = ( get_post_custom( $post-&gt;ID ) ? get_post_custom( $post-&gt;ID ) : false );</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>$custom_layout = ( isset( $custom[&#39;_oenology_layout&#39;][0] ) ? $custom[&#39;_oenology_layout&#39;][0] : &#39;default&#39; );<span class="Apple-tab-span" style="white-space:pre">        </span></div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>$layout = &#39;&#39;;</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>if ( is_page() ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>if ( &#39;default&#39; == $custom_layout ) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>$layout .= $oenology_options[&#39;default_static_page_layout&#39;];</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>} else {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                        </span>$layout .= $custom_layout;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>} else if ( is_single() ) {</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>if ( &#39;default&#39; == $custom_layout ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>$layout .= $oenology_options[&#39;default_single_post_layout&#39;];</div>
<div><span class="Apple-tab-span" style="white-space:pre">                </span>} else {</div><div><span class="Apple-tab-span" style="white-space:pre">                        </span>$layout .= $custom_layout;</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>}</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>} else if ( is_home() || is_archive() || is_search() || is_404() ) {</div><div><span class="Apple-tab-span" style="white-space:pre">                </span>$layout .= $oenology_options[&#39;post_index_layout&#39;];</div>
<div><span class="Apple-tab-span" style="white-space:pre">        </span>}</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>return $layout;</div><div>}</div></div></blockquote><div><br></div><div>Perhaps some will find this useful, I hope!</div>
<div><br></div><div>Chip</div>