在WP模板的左侧边栏/主体/右侧边栏中显示Page数据片段
我将每个页面内容分解为内容类别,例如:概览,产品详细信息和课程/材料。所有在WP中的一个页面/文章。在WP模板的左侧边栏/主体/右侧边栏中显示Page数据片段
我想在主体中显示概览数据,在左边栏中显示产品详细信息,在右边栏中显示课程/材料。
我该如何去做这件事,我已经在网上搜索,但没有找到具体的东西。
任何建议将是最有帮助的。
如果我处于您的情况,我会考虑将自己的“部分”存储在自定义字段中。我可能会提供帮助,但是能否提供到目前为止的链接?
编辑
我想自定义元领域将是最好的选择,所以我创建了一个轻量级的插件,它会添加metaboxes到您的网页,存储数据的概述,产品细节,以及课程/材料。
下面就以插件的链接MediaFire
更新日志
- 1.0初始版本
- 1.1添加窗口小部件
- 1.2固定函数调用
要调用在你的主题中的细节,您可以使用以下功能:
-
<?php get_content_overview($id) ?>
或<?php echo (get_post_meta($id, 'get_content_overview', TRUE)) ?>
//获取从 '概览' meta框的细节
-
<?php get_content_details($id) ?>
或<?php echo (get_post_meta($id, 'get_content_details', TRUE)) ?>
//获取从 '详细资料' meta框的细节
-
<?php get_content_supplements($id) ?>
或<?php echo (get_post_meta($id, 'get_content_supplements', TRUE)) ?>
//获取从 '课程/材料' 元包
插入Widget的详细信息在侧边栏中,只有正在查看的网页具有指定的元内容时,它们才会显示。
这里是原始代码(来自v1。0):
<?php
/*
Plugin Name: Content Meta Boxes (MB)
Plugin URI: http://mechabyte.com
Description: Displays custom content meta boxes.
Author: Matthew Smith
Version: 1.0
Author URI: http://mechabyte.com
*/
function get_content_overview() {
if (get_post_meta($post->ID, 'get_content_overview', TRUE)) {
echo get_post_meta($post->ID, 'get_content_overview', TRUE);
}
}
function get_content_details() {
if (get_post_meta($post->ID, 'get_content_details', TRUE)) {
echo get_post_meta($post->ID, 'get_content_details', TRUE);
}
}
function get_content_supplements() {
if (get_post_meta($post->ID, 'get_content_supplements', TRUE)) {
echo get_post_meta($post->ID, 'get_content_supplements', TRUE);
}
}
// Add the Meta Box
function add_content_boxes() {
add_meta_box(
'custom_meta_box', // $id
'Custom Content Boxes', // $title
'show_content_metabox', // $callback
'page', // $page
'normal', // $context
'high'); // $priority
}
add_action('add_meta_boxes', 'add_content_boxes');
// Field Array
$prefix = 'get_content';
$content_meta_fields = array(
array(
'label'=> 'Product Overview',
'desc' => 'Overview Data',
'id' => $prefix.'_overview',
'type' => 'textarea'
),
array(
'label'=> 'Product Details',
'desc' => 'Add some details about the product',
'id' => $prefix.'_details',
'type' => 'textarea'
),
array(
'label'=> 'Courses/Materials',
'desc' => 'Don\'t forget to add your courses and materials!',
'id' => $prefix.'_supplements',
'type' => 'textarea'
)
);
// The Callback
function show_content_metabox() {
global $content_meta_fields, $post;
// Use nonce for verification
echo '<input type="hidden" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';
// Begin the field table and loop
echo '<table class="form-table">';
foreach ($content_meta_fields as $field) {
// get value of this field if it exists for this post
$meta = get_post_meta($post->ID, $field['id'], true);
// begin a table row with
echo '<tr>
<th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
<td>';
switch($field['type']) {
// text
case 'text':
echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" />
<br /><span class="description">'.$field['desc'].'</span>';
break;
// checkbox
case 'checkbox':
echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
<label for="'.$field['id'].'">'.$field['desc'].'</label>';
break;
// textarea
case 'textarea':
echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea>
<br /><span class="description">'.$field['desc'].'</span>';
break;
} //end switch
echo '</td></tr>';
} // end foreach
echo '</table>'; // end table
}
// Save the Data
function save_content_meta($post_id) {
global $content_meta_fields;
// verify nonce
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))
return $post_id;
// check autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// check permissions
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} elseif (!current_user_can('edit_post', $post_id)) {
return $post_id;
}
// loop through fields and save the data
foreach ($content_meta_fields as $field) {
$old = get_post_meta($post_id, $field['id'], true);
$new = $_POST[$field['id']];
if ($new && $new != $old) {
update_post_meta($post_id, $field['id'], $new);
} elseif ('' == $new && $old) {
delete_post_meta($post_id, $field['id'], $old);
}
} // end foreach
}
add_action('save_post', 'save_content_meta');
?>
我没有在目前这个公布了仍处于dev的,我一直在寻找的自定义字段,但它好像你只能保存在那里的数据有限。我可能需要每个“部分”大约需要1000个字符。 – Grant 2012-02-09 20:24:57
我的另一个想法是添加一些div ID,并尝试将该数据以某种方式通过侧边栏。 – Grant 2012-02-09 20:25:46
我并非试图淡化您的想法,但我认为自定义字段将是一个更简单的选项,并且在挖掘了一段时间之后,我找不到任何提及它们允许比文章本身更少的字符:P – Matt 2012-02-09 20:29:36