在WP模板的左侧边栏/主体/右侧边栏中显示页面数据片段


Display snippets of Page data in left sidebar / main body / right sidebar of WP template

我将每个页面的内容分解为内容类别,例如:概述、产品详细信息和课程/材料。一页纸/张贴在WP中。

我想在主体中显示概览数据,在左侧边栏中显示产品细节,在右侧边栏中显示课程/材料。

我该怎么做呢?我在网上搜索过,但找不到任何具体的东西。

任何建议都会很有帮助。

如果我处于您的情况,我会考虑将您的"sections"存储在自定义字段中。我可能会帮忙,但你能提供一个链接到你目前为止所拥有的吗?

编辑

我认为自定义元字段是最好的选择,所以我创建了一个轻量级插件,可以将元框添加到您的页面中,存储概述、产品详细信息和课程/材料的数据。

以下是MediaFire 上插件的链接

变更日志

  • 1.0原始版本
  • 1.1添加的小工具
  • 1.2固定函数调用

要调用主题中的详细信息,您可以使用以下功能:

  • <?php get_content_overview($id) ?>

    <?php echo (get_post_meta($id, 'get_content_overview', TRUE)) ?>

    //从"概览"元框获取详细信息

  • <?php get_content_details($id) ?>

    <?php echo (get_post_meta($id, 'get_content_details', TRUE)) ?>

    //从"详细信息"元框获取详细信息

  • <?php get_content_supplements($id) ?>

    <?php echo (get_post_meta($id, 'get_content_supplements', TRUE)) ?>

    //从"课程/材料"元框获取详细信息

在侧边栏中插入小部件,只有当正在查看的页面具有指定的元内容时,它们才会显示。

以下是原始代码(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');  
?>