Tint添加多种文章布局

    //文章样式post_layout
    add_meta_box(
        'tt_post_post_layout',
        __('文章样式', TD),
        'tt_post_post_layout_callback',
        'post',
        'normal',
        'high'
    );
function tt_post_post_layout_callback($post)
{
    $post_layout = get_post_meta($post->ID, 'tt_post_layout', true); ?>
    <p style="width:100%;">
        <select name="tt_post_layout">
        <option value="one" <?php if ($post_layout == 'one') { echo 'selected="selected"'; } ?>>无图</option>
        <option value="two" <?php if ($post_layout == 'two') { echo 'selected="selected"'; } ?>>小图</option>
        <option value="three" <?php if ($post_layout == 'three') { echo 'selected="selected"'; } ?>>大图</option>
        <option value="four" <?php if ($post_layout == 'four') { echo 'selected="selected"'; } ?>>单栏</option>
        </select>
    </p>
<?php
}
        // 检查和更新字段
    if (isset($_POST['tt_post_layout'])) {
        update_post_meta($post_id, 'tt_post_layout', trim($_POST['tt_post_layout']));
    }
/**
 * 给Body添加额外的class(部分自定义页面无法使用wp的body_class函数生成独特的class).
 *
 * @param $classes
 *
 * @return array
 */
function tt_modify_body_classes($classes)
{
    if( is_single() ){
        $post_layout = get_post_meta( get_the_ID(), 'tt_post_layout', true );
        switch ($post_layout) {
            case 'one':
              $classes[] = 'post-style01';
              break;
            case 'two':
              $classes[] = 'post-style02';
              break;
            case 'three':
              $classes[] = 'post-style03';
              break;
            case 'four':
              $classes[] = 'post-style04';
              break;
            default:
              # code...
              break;
        }     
    }
    //TODO more
    return $classes;
}
add_filter('body_class', 'tt_modify_body_classes');

发表评论 (0)

后再参与讨论