调整WP 4.5定制器中使用的设备预览大小

时间:2016-04-14 作者:jgraup

WP 4.5介绍了Device Preview in the Customizer 而且它很容易使用。单击三个图标中的一个,以各种预先确定的大小查看您的站点。

台式机版本将始终占据您屏幕的全部空间。平板电脑版本设置为6英寸x 9英寸。移动版本设置为320px 480px。您还可以使用customize_previewable_devices

add_filter( \'customize_previewable_devices\', \'__return_empty_array\' );
有很多讨论@#31195, 但假设您可以添加/删除previewable devices 您在哪里确定这些视图的大小?

欲了解更多品种更好的原因,请参阅http://design.google.com/resizer/.

设备定位解决方案基于以下答案Luis Sanz, 我认为这个解决方案更完整一些。它涉及添加新设备、设置图标和调整列表中设备的顺序。

虽然我认为设置这些窗口的高度以显示纵向和横向设置之间的差异很有趣,但我真的认为100% 在大多数情况下,身高是最好的。

图标当前正在使用Dashicons 但我也可以看到将这些替换为建议断点的东西,而不是将来的设备。[SM, M, L, XL]

/**
 *   Determine the device view size and icons in Customizer
 */
function wpse_20160503_adjust_customizer_responsive_sizes() {

    $mobile_margin_left = \'-240px\'; //Half of -$mobile_width
    $mobile_width = \'480px\';
    $mobile_height = \'720px\';

    $mobile_landscape_width = \'720px\';
    $mobile_landscape_height = \'480px\';

    $tablet_width = \'720px\';
    $tablet_height = \'1080px\';

    $tablet_landscape_width = \'1080px\';
    $tablet_landscape_height = \'720px\';

    ?>
    <style>
        .wp-customizer .preview-mobile .wp-full-overlay-main {
            margin-left: <?php echo $mobile_margin_left; ?>;
            width: <?php echo $mobile_width; ?>;
            height: <?php echo $mobile_height; ?>;
        }

        .wp-customizer .preview-mobile-landscape .wp-full-overlay-main {

            width: <?php echo $mobile_landscape_width; ?>;
            height: <?php echo $mobile_landscape_height; ?>;
            top: 50%;
            left: 50%;
            -webkit-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
        }

        .wp-customizer .preview-tablet .wp-full-overlay-main {

            width: <?php echo $tablet_width; ?>;
            height: <?php echo $tablet_height; ?>;
        }

        .wp-customizer .preview-tablet-landscape .wp-full-overlay-main {

            width: <?php echo $tablet_landscape_width; ?>;
            height: <?php echo $tablet_landscape_height; ?>;
            top: 50%;
            left: 50%;
            -webkit-transform: translate(-50%, -50%);
            transform: translate(-50%, -50%);
        }

        .wp-full-overlay-footer .devices .preview-tablet-landscape:before {
            content: "\\f167";
        }

        .wp-full-overlay-footer .devices .preview-mobile-landscape:before {
            content: "\\f167";
        }
    </style>
    <?php

}

add_action( \'customize_controls_print_styles\', \'wpse_20160503_adjust_customizer_responsive_sizes\' );

/**
 *   Set device button settings and order
 */
function wpse_20160503_filter_customize_previewable_devices( $devices )
{
    $custom_devices[ \'desktop\' ] = $devices[ \'desktop\' ];
    $custom_devices[ \'tablet\' ] = $devices[ \'tablet\' ];
    $custom_devices[ \'tablet-landscape\' ] = array (
            \'label\' => __( \'Enter tablet landscape preview mode\' ), \'default\' => false,
    );
    $custom_devices[ \'mobile\' ] = $devices[ \'mobile\' ];
    $custom_devices[ \'mobile-landscape\' ] = array (
            \'label\' => __( \'Enter mobile landscape preview mode\' ), \'default\' => false,
    );

    foreach ( $devices as $device => $settings ) {
        if ( ! isset( $custom_devices[ $device ] ) ) {
            $custom_devices[ $device ] = $settings;
        }
    }

    return $custom_devices;
}

add_filter( \'customize_previewable_devices\', \'wpse_20160503_filter_customize_previewable_devices\' );
介质查询解决方案这里是一个利用断点的示例,如[L|M|S] 基于之前的设备定向解决方案,无需额外的图示符。这些显然是对你的主题的媒体提问的补充。

/**
 * Determine the size of the devices and icons in Customizer
 */
function wpse_20160504_adjust_customizer_responsive_sizes() {
    ?>
    <style>
        .wp-customizer .preview-small .wp-full-overlay-main {
            width: 320px;
            height: 100%;
            left: 50%;
            -webkit-transform: translateX(-50%);
            transform: translateX(-50%);
        }

        .wp-customizer .preview-medium .wp-full-overlay-main {
            width: 768px;
            height: 100%;
            left: 50%;
            -webkit-transform: translateX(-50%);
            transform: translateX(-50%);
        }

        .wp-customizer .preview-large .wp-full-overlay-main {
            width: 1224px;
            height: 100%;
            left: 50%;
            -webkit-transform: translateX(-50%);
            transform: translateX(-50%);
        }

        .wp-full-overlay-footer .devices .preview-small:before {
            content: "S";
        }

        .wp-full-overlay-footer .devices .preview-medium:before {
            content: "M";
        }

        .wp-full-overlay-footer .devices .preview-large:before {
            content: "L";
        }

    </style>
    <?php
}

add_action( \'customize_controls_print_styles\', \'wpse_20160504_adjust_customizer_responsive_sizes\' );

/**
 * Add device sizes to customizer
 */
function wpse_20160504_filter_customize_previewable_devices( $devices )
{
    $custom_devices[ \'desktop\' ] = $devices[ \'desktop\' ];
    $custom_devices[ \'large\' ] = array ( \'label\' => __( \'Large\' )  );
    $custom_devices[ \'medium\' ] = array ( \'label\' => __( \'Medium\' )  );
    $custom_devices[ \'small\' ] = array ( \'label\' => __( \'Small\' )  );

    return $custom_devices;
}

add_filter( \'customize_previewable_devices\', \'wpse_20160504_filter_customize_previewable_devices\' );

2 个回复
最合适的回答,由SO网友:Luis Sanz 整理而成

移动设备和平板电脑尺寸均在管理员手册中定义themes.css 文件触发按钮时运行的javascript只是用于添加和删除类,而不是处理大小本身。

因此,通过添加一些额外的css来覆盖维度应该不难。为了保持简单,我使用customize_controls_print_styles 内联一些样式,但也可以通过将css文件排队来完成。

<?php

    /*
        Plugin Name: Adjust Customizer responsive sizes
        Description: Allows to change the mobile and tablet preview dimensions in the WP Customizer
        Version: 0.1
        Author: Your Name
        Author URI: http://www.yourwebsite.com/
    */

    if ( ! defined( \'ABSPATH\' ) ) exit;

    function wpse_223684_adjust_customizer_responsive_sizes() {

        $mobile_margin_left = \'-240px\'; //Half of -$mobile_width
        $mobile_width = \'480px\';
        $mobile_height = \'720px\';

        $tablet_margin_left = \'-540px\'; //Half of -$tablet_width
        $tablet_width = \'1080px\';
        $tablet_height = \'720px\';

?>
        <style>
            .wp-customizer .preview-mobile .wp-full-overlay-main {
                margin-left: <?php echo $mobile_margin_left; ?>;
                width: <?php echo $mobile_width; ?>;
                height: <?php echo $mobile_height; ?>;
            }

            .wp-customizer .preview-tablet .wp-full-overlay-main {
                margin-left: <?php echo $tablet_margin_left; ?>;
                width: <?php echo $tablet_width; ?>;
                height: <?php echo $tablet_height; ?>;
            }
        </style>
<?php

    }

    add_action( \'customize_controls_print_styles\', \'wpse_223684_adjust_customizer_responsive_sizes\' );

?>
默认大小为320x480px 对于移动和720x1080px 用于平板电脑。

EDIT 16/04/26: 反映WordPress 4.5.1版本中默认平板电脑尺寸的变化。

SO网友:Ben Matthews

路易斯·桑兹的伟大解决方案。除了涉及横向和纵向查询的媒体查询外,效果非常好,因为iFrame无法缩放。为了解决这个问题,我使用了路易斯的脚本并添加了

.wp-customizer .preview-tablet .wp-full-overlay-main iframe {
    height: <?php echo $tablet_height; ?> !important;
}
.wp-customizer .preview-mobile .wp-full-overlay-main iframe {
    height: <?php echo $mobile_height; ?> !important;
}
我将此添加到本节末尾。

相关推荐