1/13/2011 – New and improved function used for retrieving custom field data for the current post called base_get_all_custom_fields(). This function will run a database query to get all custom fields attached to the current page, post or post type and store them in an array.
WordPress is great for dynamically managing the content of a website, but sometimes you encounter situations that require advanced management to handle different sections of a single page. It’s would be great if we could manage separate chunks of content with more than one WYSIWYG. Well guess what, you can, and it’s easier than you may think.
The best solution I’ve found for this situation is the Custom Field Template plugin. With this plugin you can add multiple WYSIWYG managed areas to one WordPress page or post. You can even use select boxes, radio buttons, and checkboxes to dynamically control the content of a page.
Add this under Settings > Custom Field Template > Custom Field Template Options Box
[cft_tinymce_1]
type = textarea
rows = 8
tinyMCE = true
[cft_tinymce_2]
type = textarea
rows = 8
tinyMCE = true
[cft_checkboxes_1]
type = checkbox
value = option1 # option2 # option3
Open up your theme’s functions.php file. If you don’t have one, create it. Add the following PHP function to the file.
// Get Custom Field Template Values
function get_custom_field($field) {
global $post;
$custom_field_data = get_post_meta($post->ID, $field, false);
if($custom_field_data) {
if( count($custom_field_data) > 1 ) {
return $custom_field_data;
} else {
return $custom_field_data[0];
}
} else {
return false;
}
}
Next we need to take the content created by these new fields and add it to our template. To do this we’ll need some basic PHP.
<?php
/** Get the "cft_tinymce_1" custom field */
$block_1 = get_custom_field('cft_tinymce_1');
if( $block_1 ) {
?>
<div id="block-1" class="content-box">
<h2>Block #1</h2>
<div class="entry">
<?php echo $block_1; ?>
</div>
</div>
<?php } ?>
<?php
/** Get the "cft_tinymce_2" custom field */
$block_2 = get_custom_field('cft_tinymce_2');
if( $block_2 ) {
?>
<div id="block-2" class="content-box"> <h2>Block #2</h2>
<div class="entry">
<?php echo $block_2; ?>
</div>
</div>
<?php }
Several people have mentioned that these functions do not work with checkboxes and radio buttons. These fields multiple data entries into a single custom field. I’ve written the get_custom_field() function to return fields like this as an array. You can display the values like this:
/** Get a custom field with multiple values and return as an array */
$checkboxes_1 = get_custom_field('cft_checkboxes_1');
if( $checkboxes_1 ) {
?>
<div id="block-1" class="content-box">
<h2>Custom Field (multiple)</h2>
<div class="entry">
<?php print_r($checkboxes_1); ?>
</div>
</div>
<?php }
Open up your theme’s functions.php file. If you don’t have one, create it. Add the following PHP function to the file.
// Get all custom fields attached to a page
if( !function_exists('base_get_all_custom_fields') ) {
function base_get_all_custom_fields(){
global $post;
global $wpdb;
$sql = "SELECT * FROM $wpdb->postmeta WHERE post_id = $post->ID ORDER BY meta_id ASC";
$custom_fields = $wpdb->get_results($sql);
$custom_field_array = array();
foreach($custom_fields as $field) {
$custom_field_array["$field->meta_key"] = $field->meta_value;
}
return $custom_field_array;
}
}
Next we need to take the content created by these new fields and add it to a template in your theme. To do this we’ll need some basic PHP. Open up a template file, such as page.php, in your WordPress theme and add the following:
// Get all custom fields attached to this post and store them in an array
$custom_fields = base_get_all_custom_fields();
?>
<?php if( !empty($custom_fields['cft_tinymce_1']) ) { ?>
<div id="block-1" class="content-box">
<h2>Block #1</h2>
<div class="entry">
<?php echo $custom_fields['cft_tinymce_1']; ?>
</div>
</div>
<?php } ?>
<?php if( !empty($custom_fields['cft_tinymce_2']) ) { ?>
<div id="block-2" class="content-box">
<h2>Block #2</h2>
<div class="entry">
<?php echo $custom_fields['cft_tinymce_2']; ?>
</div>
</div>
<?php }
I usually choose to create a custom page template to display these new content blocks. You can easily include these fields on every post, page or both. All you have to do is select an option in the plugin settings page (there’s a small radio button labeled “Post Type:”).
For the purpose of presentation I’ve added some basic styles into the mix. Nothing fancy.
/* Disposable Styles */
*.content-box {
padding:12px;
background:#e5e5e5;
width:281px;
float:left;
}
*.content-box h2 {
margin-top:0;
padding-bottom:6px;
border-bottom:1px solid #ccc;
}
#block-2 {
margin:0 0 0 20px;
display:inline; /* For IE6 */
}