代码高亮

· · 更新于 2025年12月21日 · 6,216 字 · 约 16 分钟
50 1

这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示这是代码高亮演示

index.php
PHP
<?php
/**
 * 主模板文件
 *
 * WordPress 主题的主要模板文件
 * 用于显示首页内容
 *
 * @package    Xun
 * @since      1.0.0
 * @author     June
 * @link       https://www.xuntheme.com
 */

// 防止直接访问
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

get_header();

// 获取侧边栏设置
$sidebar_position = xun_get_current_sidebar_position();
$has_sidebar      = xun_current_has_sidebar();
$container_class  = xun_get_sidebar_container_class( $sidebar_position, $has_sidebar );

// 获取启用的模块列表
$enabled_modules = xun_get_enabled_modules();

// 定义全宽模块(这些模块不受侧边栏布局限制)
$fullwidth_modules = apply_filters( 'xun_fullwidth_modules', [
    'banner',
    'slider',
    'topics',
    'vip',
    'stats',
] );
?>

<main id="main" class="site-main">
    <?php
    // 检查是否有启用的模块
    if ( empty( $enabled_modules ) ) :
    ?>
        <div class="xun-layout-wrapper <?php echo esc_attr( $container_class ); ?>">
            <div class="xun-layout-content">
                <div class="xun-empty-home">
                    <div class="xun-empty-content">
                        <div class="xun-empty-icon">
                            <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="M4 5a1 1 0 011-1h14a1 1 0 011 1v2a1 1 0 01-1 1H5a1 1 0 01-1-1V5zM4 13a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H5a1 1 0 01-1-1v-6zM16 13a1 1 0 011-1h2a1 1 0 011 1v6a1 1 0 01-1 1h-2a1 1 0 01-1-1v-6z"></path>
                            </svg>
                        </div>
                        <h2 class="xun-empty-title"><?php esc_html_e( '首页模块未配置', 'xun' ); ?></h2>
                        <p class="xun-empty-desc"><?php esc_html_e( '请前往主题设置 → 综合设置 → 首页设置,启用并配置首页模块', 'xun' ); ?></p>
                        <?php if ( current_user_can( 'manage_options' ) ) : ?>
                            <a href="<?php echo esc_url( admin_url( 'admin.php?page=xun-options#tab=首页设置' ) ); ?>" class="btn btn-primary btn-sm">
                                <svg fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path>
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"></path>
                                </svg>
                                <?php esc_html_e( '前往设置', 'xun' ); ?>
                            </a>
                        <?php endif; ?>
                    </div>
                </div>
            </div>
        </div>
    <?php
    else :
        /**
         * 首页内容前触发
         *
         * @since 1.0.0
         * @hook  xun_before_home_content
         */
        do_action( 'xun_before_home_content' );
        
        // 分离全宽模块和需要侧边栏的模块
        $in_sidebar_section = false;
        
        foreach ( $enabled_modules as $module ) :
            $is_fullwidth = in_array( $module, $fullwidth_modules, true );
            
            // 如果是全宽模块
            if ( $is_fullwidth ) :
                // 如果之前在侧边栏区域,先关闭它
                if ( $in_sidebar_section ) :
                    ?>
                    </div>
                    <?php if ( $has_sidebar ) : ?>
                    <aside class="xun-layout-sidebar xun-sidebar">
                        <?php dynamic_sidebar( 'sidebar-home' ); ?>
                    </aside>
                    <?php endif; ?>
                </div>
                    <?php
                    $in_sidebar_section = false;
                endif;
                
                // 渲染全宽模块
                do_action( "xun_before_module_{$module}" );
                get_template_part( 'template-parts/components/' . $module );
                do_action( "xun_after_module_{$module}" );
                
            else :
                // 非全宽模块,需要在侧边栏布局内
                if ( ! $in_sidebar_section ) :
                    // 开始侧边栏区域
                    ?>
                <div class="xun-layout-wrapper <?php echo esc_attr( $container_class ); ?>">
                    <div class="xun-layout-content">
                    <?php
                    $in_sidebar_section = true;
                endif;
                
                // 渲染模块
                do_action( "xun_before_module_{$module}" );
                get_template_part( 'template-parts/components/' . $module );
                do_action( "xun_after_module_{$module}" );
            endif;
        endforeach;
        
        // 如果最后还在侧边栏区域,关闭它
        if ( $in_sidebar_section ) :
            ?>
            </div>
            <?php if ( $has_sidebar ) : ?>
            <aside class="xun-layout-sidebar xun-sidebar">
                <?php dynamic_sidebar( 'sidebar-home' ); ?>
            </aside>
            <?php endif; ?>
        </div>
        <?php
        endif;
        
        /**
         * 首页内容后触发
         *
         * @since 1.0.0
         * @hook  xun_after_home_content
         */
        do_action( 'xun_after_home_content' );
    endif;
    ?>
</main>

<?php
get_footer();

1 条评论

  1. 微信用户6d87 LV1

    🤑

    回复 微信用户6d87
微信二维码
微信号: 123456789
QQ: 8740335