<?php

$luntan_url_guanli = get_option('luntan_url_guanli');
if (isset($luntan_url_guanli['list']) && $luntan_url_guanli['list']) {
  $luntan_url = $luntan_url_guanli['list'];
  $luntan_url_zx = $luntan_url_guanli['zhongxin'];
} else {
  $luntan_url = 'ltwdsqht';
  $luntan_url_zx = 'ltwdsqht_zx';
}
global $wpdb;


$categories = get_terms(array(
  'taxonomy'   => 'luntan_cate', // 自定义分类法的名称
  'hide_empty' => false,         // 是否隐藏没有关联文章的分类项
));
// 初始化分页参数
$page = get_query_var('paged') ? (int)get_query_var('paged') : 1;
$limit = 16;
$start = ($page - 1) * $limit;

// 获取置顶文章ID和搜索关键词
$sticky_posts = get_option('sticky_posts') ?: [];
$search = isset($_GET['s']) ? sanitize_text_field(wp_unslash($_GET['s'])) : '';

// 初始化变量
$list = [];
$count = 0;
global $wpdb;

/**
 * 生成置顶文章排序的SQL片段和参数
 * @param array $sticky_posts 置顶文章ID数组
 * @return array [排序SQL, 参数数组]
 */
function get_sticky_order_data($sticky_posts)
{
  if (empty($sticky_posts)) {
    return ['', []];
  }

  $ids = array_map('intval', $sticky_posts);
  $placeholders = implode(',', array_fill(0, count($ids), '%d'));
  $order_sql = "CASE WHEN p.ID IN ({$placeholders}) THEN 0 ELSE 1 END ASC,";

  return [$order_sql, $ids];
}

// 分类页处理
if (is_tax('luntan_cate')) {


  $current_term = get_queried_object();


  // 验证分类对象有效性
  if (empty($current_term) || !isset($current_term->term_id)) {

    $list = [];
    $count = 0;
  } else {
    $term_id = (int)$current_term->term_id;

    list($sticky_order_sql, $sticky_params) = get_sticky_order_data($sticky_posts);


    // 1. 先查询该分类下是否有文章（基础验证）
    $basic_check = $wpdb->get_var($wpdb->prepare(
      "SELECT COUNT(*) FROM {$wpdb->term_relationships} 
             WHERE term_taxonomy_id = (SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} 
                                      WHERE taxonomy = 'luntan_cate' AND term_id = %d)",
      $term_id
    ));


    // 2. 计算符合条件的发布文章总数
    $count = $wpdb->get_var($wpdb->prepare(
      "SELECT COUNT(DISTINCT p.ID)
            FROM {$wpdb->posts} p
            INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
            INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
            WHERE p.post_status = 'publish'
              AND p.post_type = 'luntan'
              AND tt.taxonomy = 'luntan_cate'
              AND tt.term_id = %d",
      $term_id
    ));


    // 3. 构建查询参数（严格控制顺序）
    $query_params = [
      $term_id, // 对应tt.term_id = %d
      ...$sticky_params, // 对应置顶文章ID的占位符
      $start, // LIMIT %d
      $limit  // , %d
    ];


    // 4. 构建完整SQL（便于调试）
    $sql = $wpdb->prepare(
      "SELECT p.*, MAX(c.comment_date_gmt) as latest_comment_date
            FROM {$wpdb->posts} p
            LEFT JOIN {$wpdb->comments} c ON p.ID = c.comment_post_ID
            INNER JOIN {$wpdb->term_relationships} tr ON p.ID = tr.object_id
            INNER JOIN {$wpdb->term_taxonomy} tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
            WHERE p.post_status = 'publish'
              AND p.post_type = 'luntan'
              AND tt.taxonomy = 'luntan_cate'
              AND tt.term_id = %d
            GROUP BY p.ID
            ORDER BY 
                {$sticky_order_sql}
                latest_comment_date DESC, 
                p.ID DESC 
            LIMIT %d, %d",
      $query_params
    );


    // 5. 获取帖子列表
    $list = $wpdb->get_results($sql, ARRAY_A);
  }
}
// 搜索或默认列表处理
else {
  // 保持与之前相同的搜索和默认列表逻辑
  list($sticky_order_sql, $sticky_params) = get_sticky_order_data($sticky_posts);
  $query_params = [];
  $search_conditions = '';

  if ($search) {
    $search_conditions = "AND p.post_title LIKE %s";
    $query_params[] = '%' . $search . '%';

    $count = $wpdb->get_var($wpdb->prepare(
      "SELECT COUNT(DISTINCT p.ID)
            FROM {$wpdb->posts} p
            WHERE p.post_status = 'publish'
              AND p.post_type = 'luntan'
              {$search_conditions}",
      $query_params
    ));
  } else {
    $count = $wpdb->get_var(
      "SELECT COUNT(DISTINCT p.ID)
            FROM {$wpdb->posts} p
            WHERE p.post_status = 'publish'
              AND p.post_type = 'luntan'"
    );
  }

  $query_params = array_merge(
    $query_params,
    $sticky_params,
    [$start, $limit]
  );

  $list = $wpdb->get_results($wpdb->prepare(
    "SELECT p.*, MAX(c.comment_date_gmt) as latest_comment_date
        FROM {$wpdb->posts} p
        LEFT JOIN {$wpdb->comments} c ON p.ID = c.comment_post_ID
        WHERE p.post_status = 'publish'
          AND p.post_type = 'luntan'
          {$search_conditions}
        GROUP BY p.ID
        ORDER BY 
            {$sticky_order_sql}
            latest_comment_date DESC, 
            p.ID DESC 
        LIMIT %d, %d",
    $query_params
  ), ARRAY_A);
}





if (wp_cache_get('luntan_news') === false) {
  // 执行 SQL 查询
  $news = $wpdb->get_results(" 
        SELECT p.*, MAX(c.comment_date) as latest_comment_date
        FROM {$wpdb->posts} p
        LEFT JOIN {$wpdb->comments} c ON p.ID = c.comment_post_ID
        WHERE p.post_type = 'luntan'
        AND p.post_status = 'publish'
         AND c.comment_approved = '1'
        GROUP BY p.ID
        ORDER BY latest_comment_date DESC
        LIMIT 5
    ");
  wp_cache_set('luntan_news', $news, '', 3600);
} else {
  $news = wp_cache_get('luntan_news');
}
$hot = get_posts(array(
  'numberposts' => 5,
  'orderby'     => 'meta_value_num',
  'order'       => 'luntan_views',
  'post_type' => 'luntan',
  'post_status' => 'publish',
));
?>
<!-- 最大盒子 -->
<div id="wzt_luantan_index_box">
  <?php if (!wp_is_mobile()) { ?>
    <!-- 左 -->
    <div class="fb_left">
      <!-- 头部 -->
      <div class="fbl_title">
        <!-- 分类 -->
        <div class="fblt_left">
          <!-- 头部 分类项 -->
          <div class="fbltl_item fbltl_item_act" data-id="0">
            <div class="text"><a href="/<?php echo esc_attr($luntan_url); ?>">全部分类</a></div>
            <div class="border"></div>
          </div>
          <?php if (!empty($categories)) {
            foreach ($categories as $k => $v) {
          ?>
              <div class="fbltl_item" data-id="<?php echo intval($v->term_id); ?>">
                <div class="text"><a href="<?php echo esc_url(get_term_link($v->term_id)); ?>"><?php echo esc_attr($v->name); ?></a></div>
                <div class="border"></div>
              </div>
          <?php }
          } ?>
        </div>
        <?php if (is_user_logged_in()) { ?>
          <!-- 发布按钮 -->
          <div class="fblt_right">
            <button>发布帖子</button>
          </div>
        <?php } ?>
      </div>

      <!-- 帖子列表 -->
      <div class="fbl_content" data-id="0">
        <?php

        if (!empty($list)) {

          foreach ($list as $k => $v) {
        ?>
            <!-- 帖子盒子 -->
            <div class="tz_item">
              <a href="<?php the_permalink($v['ID']); ?>">
                <!-- 帖子顶部 -->
                <div class="tz_top">
                  <!-- 帖子标题 -->
                  <div class="title"><?php echo esc_attr($v['post_title']); ?></div>
                  <!-- 置顶标签 -->

                  <!-- <div class="zhiding" :style="{ display: item.top ? 'block' : 'none' }">
            置顶
          </div> -->
                  <?php if (is_sticky($v['ID'])) { ?>
                    <div class="zhiding" style=" display: block">
                      置顶
                    </div>
                  <?php } ?>
                  <!-- 精华标签 -->
                  <!-- <div class="jing" :style="{ display: item.jing ? 'block' : 'none' }">
            精
          </div> -->
                  <?php if (get_post_meta($v['ID'], 'luntan_jing', true)) { ?>
                    <div class="jing" style=" display: block">
                      精
                    </div>
                  <?php } ?>
                </div>
                <!-- 帖子底部 -->
                <div class="tz_bom">
                  <!-- 左边 -->
                  <div class="tzb_left">
                    <!-- 发布者 -->
                    <div class="fb_box">
                      <!-- 头像 -->
                      <div class="avatar">
                        <img src="<?php echo  get_user_meta($v['post_author'], 'luntan_avatar', true) ? esc_attr(get_user_meta($v['post_author'], 'luntan_avatar', true)) : esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>" alt="" onerror="this.onerror=null;this.src='<?php echo esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>'" />
                      </div>
                      <!-- 昵称 -->
                      <div class="nick">
                        <?php
                        $user_data = get_userdata($v['post_author']);
                        if ($user_data && is_object($user_data) && property_exists($user_data, 'nickname')) {
                          echo esc_attr($user_data->nickname);
                        } else {
                          echo '用户' . (int)$v['post_author'];
                        }
                        ?>
                      </div>
                      <!-- 时间 -->
                      <div class="time">发布于<?php echo esc_attr($v['post_date']); ?></div>
                    </div>
                    <?php $comment1 = get_comments(array(
                      'post_id' => $v['ID'],
                      'number'  => 1,
                      'orderby' => 'comment_date_gmt',
                      'order'   => 'DESC',
                    ));
                    if (!empty($comment1)) {
                    ?>
                      <!-- 最后回复 -->
                      <!-- <div class="hf_box" v-if="item.comments.length > 0"> -->

                      <div class="hf_box">
                        <!-- 头像 -->
                        <div class="avatar">
                          <img src="<?php echo get_user_meta($comment1[0]->user_id, 'luntan_avatar', true) ? esc_attr(get_user_meta($comment1[0]->user_id, 'luntan_avatar', true)) : esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>" alt="" onerror="this.onerror=null;this.src='<?php echo esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>'"/>
                        </div>
                        <!-- 昵称 -->
                        <div class="nick">
                          <?php if (isset($comment1[0]->user_id)) {
                            echo get_userdata($comment1[0]->user_id)->nickname ? esc_attr(get_userdata($comment1[0]->user_id)->nickname) : esc_attr('用户' . (int)$comment1[0]->user_id);
                          } ?>
                        </div>
                        <!-- 时间 -->
                        <div class="time">最后回复于<?php echo isset($comment1[0]->comment_date) ? esc_attr($comment1[0]->comment_date) : ''; ?></div>
                      </div>
                    <?php } ?>
                  </div>

                  <!-- 右边数据 -->
                  <div class="tzb_right">
                    <!-- 浏览 -->
                    <div class="view">
                      <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/view.png', LUNTAN_FILE)); ?>" alt="" />
                      <div class="num"><?php echo get_post_meta($v['ID'], 'luntan_views', true) ? (int)get_post_meta($v['ID'], 'luntan_views', true) : 0; ?></div>
                    </div>
                    <!-- 评论 -->
                    <div class="pinglun">
                      <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/remark.png', LUNTAN_FILE)); ?>" alt="" />
                      <div class="num"><?php echo get_post_meta($v['ID'], 'luntan_replay_num', true) ? (int)get_post_meta($v['ID'], 'luntan_replay_num', true) : 0; ?></div>
                    </div>
                  </div>
                </div>
              </a>
            </div>

          <?php }
        } else { ?>
          <!-- 当没有帖子时 -->
          <div class="fblc_null" style="">
            <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/tiez_null.png', LUNTAN_FILE)); ?>" alt="" />
            <span>暂时没有帖子</span>
          </div>
        <?php } ?>
        <?php if (ceil($count / 16) > 1) {
          $pagesize = ceil($count / 16);
        ?>
          <!-- 分页器 -->
          <div class="fenyeqi">
            <ul class="fenyeqi_ul">
              <?php if ($page > 1) { ?>
                <li><a href="/<?php echo esc_attr($luntan_url); ?>/page/<?php echo (int)$page - 1; ?>" class="disabled">&lt;</a></li>
              <?php } ?>
              <?php for ($i = 1; $i <= $pagesize; $i++) { ?>

                <li><a href="/<?php echo esc_attr($luntan_url); ?>/page/<?php echo (int)$i; ?>" <?php if ($page == $i) {
                                                                                                  echo 'class="active"';
                                                                                                } ?>><?php echo (int)$i; ?></a></li>
              <?php } ?>
              <?php if ($page < $pagesize) { ?>
                <li><a href="/<?php echo esc_attr($luntan_url); ?>/page/<?php echo (int)$page + 1; ?>">&gt;</a></li>
              <?php } ?>
            </ul>
          </div>
        <?php } ?>
      </div>

    </div>
  <?php } else { ?>
      <!-- 左 -->
      <div class="fb_left">
      <!-- 头部 -->
      <div class="fbl_title">
        <!-- 分类 -->
        <div class="fblt_left">
          <!-- 头部 分类项 -->
          <div class="fbltl_item fbltl_item_act" data-id="0">
            <div class="text"><a href="/<?php echo esc_attr($luntan_url); ?>">全部分类</a></div>
            <div class="border"></div>
          </div>
          <?php if (!empty($categories)) {
            foreach ($categories as $k => $v) {
          ?>
              <div class="fbltl_item" data-id="<?php echo intval($v->term_id); ?>">
                <div class="text"><a href="<?php echo esc_url(get_term_link($v->term_id)); ?>"><?php echo esc_attr($v->name); ?></a></div>
                <div class="border"></div>
              </div>
          <?php }
          } ?>
        </div>
        <?php if (is_user_logged_in()) { ?>
          <!-- 发布按钮 -->
          <div class="fblt_right">
            <button>发布帖子</button>
          </div>
        <?php } ?>
      </div>

      <!-- 手机端帖子列表 -->
      <div class="mb_fbl_content" data-id="1">
        <?php if (!empty($list)) { ?>
          <?php foreach ($list as $k => $v) { ?>
            <a class="mb_fblc_item" href="<?php the_permalink($v['ID']); ?>">
              <!-- 顶部 -->
              <div class="mfblc_top">
                <div class="left">
                  <div class="avatar">
                    <img src="<?php echo get_user_meta($v['post_author'], 'luntan_avatar', true) ? esc_attr(get_user_meta($v['post_author'], 'luntan_avatar', true)) : esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>" alt="">
                  </div>
                  <div class="data">
                    <div class="nick">
                      <?php echo get_userdata($v['post_author'])->nickname ? esc_attr(get_userdata($v['post_author'])->nickname) : '用户' . (int)$v['post_author']; ?>
                    </div>
                    <div class="fb_time">发布于<?php echo isset($v['post_date']) ? esc_html($v['post_date']) : ''; ?></div>
                  </div>
                </div>
                <div class="right">
                  <?php if (is_sticky($v['ID'])) { ?>
                    <div class="zhiding" style="display:block">置顶</div>
                  <?php } ?>
                  <?php if (get_post_meta($v['ID'], 'luntan_jing', true)) { ?>
                    <div class="jing" style="display:block">精</div>
                  <?php } ?>
                </div>
              </div>

              <div class="mfblc_con">
                <div class="title"><?php echo esc_html($v['post_title']); ?></div>
                <div class="imgs">
                  <?php
                  $luntan_img = get_post_meta($v['ID'], 'luntan_img', true);
                  $luntan_img = $luntan_img ? explode(',', $luntan_img) : [];
                  foreach ($luntan_img as $img) { ?>
                    <img src="<?php echo esc_url($img); ?>" alt="<?php echo esc_attr($v['post_title']); ?>">
                  <?php } ?>
                </div>
              </div>

              <div class="mfblc_tag">
                <?php
                $cate = get_the_terms($v['ID'], 'luntan_cate') ?: [];
                foreach ($cate as $ca) { ?>
                  <div class="tag"><?php echo esc_html($ca->name); ?></div>
                <?php } ?>
              </div>

              <div class="mfblc_bom">
                <?php
                // 获取该帖子的最新评论
                $comment1 = get_comments(array(
                  'post_id' => $v['ID'],
                  'number'  => 1,
                  'orderby' => 'comment_date_gmt',
                  'order'   => 'DESC',
                ));
                ?>

                <?php if (!empty($comment1)) { ?>
                  <div class="left">最后回复于 <?php echo esc_html(gmdate('Y-m-d H:i:s', strtotime($comment1[0]->comment_date))); ?></div>
                <?php } else { ?>
                  <div class="left">暂无回复</div>
                <?php } ?>
                <!-- <div class="left">最后回复于<?php echo isset($comment1[0]->comment_date) ? esc_attr($comment1[0]->comment_date) : ''; ?></div> -->
                <div class="right">
                  <div class="view">
                    <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/view.png', LUNTAN_FILE)); ?>" alt="" />
                    <div class="num"><?php echo (int)(get_post_meta($v['ID'], 'luntan_views', true) ?: 0); ?></div>
                  </div>
                  <div class="pinglun">
                    <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/remark.png', LUNTAN_FILE)); ?>" alt="" />
                    <div class="num"><?php echo (int)(get_post_meta($v['ID'], 'luntan_replay_num', true) ?: 0); ?></div>
                  </div>
                </div>
              </div>
            </a>
          <?php } ?>

          <?php if (ceil($count / 16) > 1) {
            $pagesize = ceil($count / 16);
          ?>
            <div class="fenyeqi">
              <ul class="fenyeqi_ul">
                <?php if ($page > 1) { ?>
                  <li><a href="/<?php echo esc_attr($luntan_url); ?>/page/<?php echo (int)$page - 1; ?>">&lt;</a></li>
                <?php } ?>
                <?php for ($i = 1; $i <= $pagesize; $i++) { ?>
                  <li><a href="/<?php echo esc_attr($luntan_url); ?>/page/<?php echo (int)$i; ?>" <?php if ($page == $i) echo 'class="active"'; ?>><?php echo (int)$i; ?></a></li>
                <?php } ?>
                <?php if ($page < $pagesize) { ?>
                  <li><a href="/<?php echo esc_attr($luntan_url); ?>/page/<?php echo (int)$page + 1; ?>">&gt;</a></li>
                <?php } ?>
              </ul>
            </div>
          <?php } ?>

        <?php } else { ?>
          <!-- 无帖子时展示 -->
          <div class="fblc_null">
            <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/tiez_null.png', LUNTAN_FILE)); ?>" alt="">
            <span>暂时没有帖子</span>
          </div>
        <?php } ?>
      </div>

    </div>
  <?php }
  ?>

  <!-- 右 -->
  <div class="fb_right">
    <!-- 个人信息栏 -->
    <div class="fbr_userInfo">
      <?php if (is_user_logged_in()) {
        $current_user = wp_get_current_user();
        //"您已登录！";
      ?>
        <div class="fbr_u_top">
          <div class="fbr_u_t_left">
            <div class="fbr_u_t_l_avatar">

              <!-- 默认头像 -->
              <img src="<?php echo get_user_meta($current_user->ID, 'luntan_avatar', true) ? esc_attr(get_user_meta($current_user->ID, 'luntan_avatar', true)) : esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>" alt="" onerror="this.onerror=null;this.src='<?php echo esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>'"/>
             
            </div>
            <div class="fbr_u_t_l_info">
              <!-- 已经登录 -->
              <div class="fbr_u_t_l_i_nick"><?php if (isset($current_user->display_name)) {
                                              echo esc_attr($current_user->display_name);
                                            } ?></div>

              <!-- 已经登录 -->
              <div class="fbr_u_t_l_i_id">ID：<?php if (isset($current_user->ID)) {
                                                echo intval($current_user->ID);
                                              } ?></div>
            </div>
          </div>
          <!-- 已经登录 -->
          <div class="fbr_u_t_right">
            <?php $pub_num = count(get_posts(array(
              'author' => $current_user->ID,
              'numberposts' => -1, // 获取所有符合条件的文章，-1表示无限制数量
              'post_type' => 'luntan',
            )));
            ?>
            <div class="fbr_u_t_r_num"><?php echo (int)$pub_num; ?></div>
            <div class="fbr_u_t_r_text">帖子</div>
          </div>
        </div>
        <div class="fbr_u_bom">

          <!-- 已经登录 -->
          <div class="fbr_u_true">
            <div class="fbr_u_b_t_left">
              <a href="/<?php echo esc_attr($luntan_url_zx); ?>">
                <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/user.png', LUNTAN_FILE)); ?>" alt="" />
                <span>个人中心</span>
              </a>
            </div>
            <div class="fbr_u_b_t_right">
              <a href="<?php echo esc_url(wp_logout_url(home_url('/' . $luntan_url))); ?>">
                <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/goout.png', LUNTAN_FILE)); ?>" alt="" />
                <span>退出登录</span>
              </a>
            </div>
          </div>

        </div>

      <?php } else { ?>
        <?php // "您未登录，请先登录。";
        ?>
        <div class="fbr_u_top">
          <div class="fbr_u_t_left">
            <div class="fbr_u_t_l_avatar">

              <!-- 默认头像 -->
              <img src="<?php echo  esc_attr(plugins_url('inc/index/assets/img/avatar_default.png', LUNTAN_FILE)); ?>" alt="" />
            </div>
            <div class="fbr_u_t_l_info">

              <!-- 未登录 -->
              <div class="fbr_u_t_l_i_nick">请先登录</div>
              <!-- 已经登录 -->

            </div>
          </div>

        </div>
        <div class="fbr_u_bom">
          <!-- 未登录 -->
          <a href="<?php echo esc_url(wp_login_url()); ?>" class="fbr_u_false">
            <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/user.png', LUNTAN_FILE)); ?>" alt="" />
            <span>请先登录</span>
          </a>
        </div>

      <?php } ?>

    </div>

    <!-- 搜索 -->
    <div class="fbr_search">
      <form action="/<?php echo esc_attr($luntan_url); ?>/" method="get">
        <input type="text" type="text" class="el-input__inner" placeholder="请输入搜索内容" style="padding: 0 10px 0 25px  !important; border: 1px solid #DCDFE6 !important;" name="s">
        <button class="el-button" style="background-color: #2281ff !important;">
          搜索
        </button>
        <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/search.png', LUNTAN_FILE)); ?>" alt="" />
      </form>
    </div>
    <?php if (!wp_is_mobile()) { ?>
      <!-- 热门 -->
      <div class="fbr_hot">
        <!-- 热门标题 -->
        <div class="h_tit">热门帖子33</div>
        <!-- 热门帖子列表 -->
        <div class="h_list">
          <!-- 热门帖子盒子 -->
          <?php if (!empty($hot)) {
            foreach ($hot as $k => $v) {
          ?>
              <div class="h_item">
                <?php
                // 定义对应关系
                $xuhao_classes = ['one', 'two', 'three', 'four', 'five'];
                // 根据索引循环取对应的类
                $xuhao_class = $xuhao_classes[$k % 5];
                ?>
                <a href="<?php if (isset($v->ID)) {
                            the_permalink($v->ID);
                          } ?>">
                  <!-- 头部 -->
                  <div class="hi_top">
                    <div class="'xuhao <?php echo esc_attr($xuhao_class); ?>">
                      <?php echo (int)$k + 1; ?>
                    </div>
                    <div class="title"><?php echo esc_attr($v->post_title); ?></div>
                  </div>
                  <!-- 底部 -->
                  <div class="hi_bom">
                    <!-- 相关数据 -->
                    <div class="hb_left">
                      <!-- 浏览 -->
                      <div class="view">
                        <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/view.png', LUNTAN_FILE)); ?>" alt="" />
                        <div class="num"><?php echo get_post_meta($v->ID, 'luntan_views', true) ? (int)get_post_meta($v->ID, 'luntan_views', true) : 0; ?></div>
                      </div>
                      <!-- 评论 -->
                      <div class="pinglun">
                        <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/remark.png', LUNTAN_FILE)); ?>" alt="" />
                        <div class="num"><?php echo get_post_meta($v->ID, 'luntan_replay_num', true) ? (int)get_post_meta($v->ID, 'luntan_replay_num', true) : 0; ?></div>
                      </div>
                    </div>
                    <!-- 时间 -->
                    <div class="time"><?php if (isset($v->post_date)) {
                                        echo esc_attr($v->post_date);
                                      } ?></div>
                  </div>
                  <!-- 边框线条 -->
                  <div class="hi_border"></div>
                </a>
              </div>
          <?php }
          } ?>
        </div>
      </div>

      <!-- 最新 -->
      <div class="fbr_new">
        <!-- 最新标题 -->
        <div class="n_tit">最新回复</div>
        <!-- 热门帖子列表 -->
        <div class="n_list">
          <!-- 热门帖子盒子 -->
          <?php if (!empty($news)) {
            foreach ($news as $k => $v) {
          ?>
              <div class="n_item">
                <?php
                // 定义对应关系
                $xuhao_classes = ['one', 'two', 'three', 'four', 'five'];
                // 根据索引循环取对应的类
                $xuhao_class = $xuhao_classes[$k % 5];
                ?>
                <a href="<?php if (isset($v->ID)) {
                            the_permalink($v->ID);
                          } ?>">
                  <!-- 头部 -->
                  <div class="ni_top">
                    <div class="xuhao <?php echo esc_attr($xuhao_class); ?>">
                      <?php echo (int)$k + 1; ?>
                    </div>
                    <div class="title"><?php echo esc_attr($v->post_title); ?></div>
                  </div>
                  <!-- 底部 -->
                  <div class="ni_bom">
                    <!-- 相关数据 -->
                    <div class="nb_left">
                      <!-- 浏览 -->
                      <div class="view">
                        <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/view.png', LUNTAN_FILE)); ?>" alt="" />
                        <div class="num"><?php echo get_post_meta($v->ID, 'luntan_views', true) ? (int)get_post_meta($v->ID, 'luntan_views', true) : 0; ?></div>
                      </div>
                      <!-- 评论 -->
                      <div class="pinglun">
                        <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/remark.png', LUNTAN_FILE)); ?>" alt="" />
                        <div class="num"><?php echo get_post_meta($v->ID, 'luntan_replay_num', true) ? (int)get_post_meta($v->ID, 'luntan_replay_num', true) : 0; ?></div>
                      </div>
                    </div>
                    <!-- 时间 -->
                    <div class="time"><?php if (isset($v->post_date)) {
                                        echo esc_attr($v->post_date);
                                      } ?></div>
                  </div>
                  <!-- 边框线条 -->
                  <div class="ni_border"></div>
                </a>
              </div>
          <?php }
          } ?>
        </div>
      </div>

      <!-- 其他 -->
      <div class="fbr_other">
        <div class="o_tit">其他板块</div>


        <div class="o_type">
          <?php if (!empty($categories)) {
            foreach ($categories as $k => $v) {
          ?>
              <a href="<?php echo esc_url(get_term_link($v->term_id)); ?>">
                <div class="ot_item" data-id="<?php echo intval($v->term_id); ?>">
                  <?php echo esc_attr($v->name); ?>
                </div>
              </a>
          <?php }
          } ?>
        </div>

      </div>
  </div>
<?php } ?>
<!-- 发布帖子弹出框 -->
<div class="fbDialogBox">
  <div class="dialog-header">
    <span>发布新帖子</span>
    <div class="close-btn">&times;</div>
  </div>

  <div class="dialog-body">
    <div class="form-item">
      <div class="bold_text">标题</div>
      <input type="text" class="title_input" placeholder="请输入标题" style="padding: 10px  !important; border: 1px solid #DCDFE6 !important;">
    </div>
    <div class="form-item">
      <div class="bold_text">内容</div>
      <textarea class="content_input" placeholder="请输入内容"></textarea>
    </div>

    <div class="form-item">
      <div class="bold_text">添加照片</div>
      <div class="wztimgBox">
        <div class="addImg-btn">
          <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="32" height="32" viewBox="0 0 32 32" fill="none">
            <path d="M13.3333 13.3333L2.67333 13.3333C2.62963 13.3333 2.58596 13.3343 2.54231 13.3363C2.49866 13.3384 2.45509 13.3416 2.41159 13.3458C2.36809 13.35 2.32473 13.3553 2.28149 13.3617C2.23826 13.368 2.1952 13.3754 2.15233 13.3839C2.10946 13.3923 2.06682 13.4018 2.02442 13.4124C1.98201 13.423 1.93989 13.4346 1.89806 13.4472C1.85622 13.4598 1.81472 13.4735 1.77355 13.4881C1.73239 13.5028 1.69161 13.5185 1.65121 13.5351C1.61082 13.5518 1.57085 13.5694 1.53132 13.5881C1.49179 13.6067 1.45274 13.6263 1.41418 13.6468C1.37561 13.6674 1.33757 13.6889 1.30006 13.7113C1.26255 13.7337 1.22561 13.757 1.18924 13.7812C1.15287 13.8055 1.11713 13.8306 1.08199 13.8566C1.04686 13.8826 1.01238 13.9094 0.978566 13.9371C0.94475 13.9648 0.911634 13.9932 0.879219 14.0226C0.846803 14.0518 0.815125 14.0819 0.784184 14.1128C0.753247 14.1437 0.723084 14.1753 0.693697 14.2076C0.664312 14.24 0.635737 14.273 0.607972 14.3068C0.580206 14.3405 0.553288 14.3749 0.527216 14.41C0.501141 14.445 0.475944 14.4807 0.451622 14.517C0.4273 14.5533 0.403884 14.5902 0.381375 14.6277C0.358866 14.6652 0.337287 14.7031 0.316644 14.7416C0.296 14.7802 0.276313 14.8192 0.257584 14.8586C0.238855 14.8981 0.221107 14.938 0.204338 14.9784C0.18757 15.0187 0.171802 15.0595 0.157034 15.1006C0.142267 15.1418 0.128517 15.1832 0.115787 15.225C0.103056 15.2668 0.0913581 15.3089 0.0806944 15.3513C0.0700306 15.3937 0.0604134 15.4363 0.0518428 15.4791C0.0432719 15.522 0.0357578 15.565 0.0293007 15.6082C0.0228435 15.6515 0.0174508 15.6948 0.0131228 15.7383C0.00879478 15.7818 0.00553653 15.8253 0.00334806 15.869C0.00115961 15.9126 4.35859e-05 15.9563 0 16C0 17.4827 1.19733 18.6667 2.67333 18.6667L13.3333 18.6667L13.3333 29.3267C13.3333 30.804 14.5267 32 16 32C17.4827 32 18.6667 30.8027 18.6667 29.3267L18.6667 18.6667L29.3267 18.6667C29.3704 18.6667 29.414 18.6658 29.4577 18.6637C29.5013 18.6616 29.5449 18.6584 29.5884 18.6542C29.6319 18.65 29.6753 18.6447 29.7185 18.6383C29.7617 18.632 29.8048 18.6246 29.8477 18.6161C29.8905 18.6077 29.9332 18.5982 29.9756 18.5876C30.018 18.577 30.0601 18.5654 30.1019 18.5528C30.1438 18.5402 30.1853 18.5265 30.2265 18.5119C30.2676 18.4972 30.3084 18.4815 30.3488 18.4649C30.3892 18.4482 30.4292 18.4306 30.4687 18.4119C30.5082 18.3933 30.5473 18.3737 30.5858 18.3532C30.6244 18.3326 30.6624 18.3111 30.6999 18.2887C30.7375 18.2663 30.7744 18.243 30.8107 18.2188C30.8471 18.1945 30.8829 18.1694 30.918 18.1434C30.9532 18.1174 30.9876 18.0906 31.0214 18.0629C31.0553 18.0352 31.0884 18.0068 31.1208 17.9774C31.1532 17.9482 31.1849 17.9181 31.2158 17.8872C31.2467 17.8563 31.2769 17.8248 31.3062 17.7924C31.3356 17.7601 31.3644 17.727 31.3922 17.6933C31.42 17.6595 31.4469 17.6251 31.4728 17.59C31.4988 17.555 31.5241 17.5193 31.5484 17.483C31.5728 17.4467 31.5963 17.4098 31.6187 17.3723C31.6413 17.3349 31.6628 17.2969 31.6834 17.2584C31.7041 17.2198 31.7237 17.1808 31.7425 17.1414C31.7612 17.1019 31.7791 17.062 31.7956 17.0216C31.8125 16.9813 31.8284 16.9405 31.8431 16.8994C31.8578 16.8582 31.8716 16.8168 31.8844 16.775C31.8972 16.7332 31.9088 16.6911 31.9194 16.6487C31.93 16.6063 31.9397 16.5637 31.9481 16.5209C31.9566 16.478 31.9641 16.435 31.9706 16.3918C31.9772 16.3486 31.9825 16.3052 31.9869 16.2617C31.9912 16.2183 31.9944 16.1747 31.9966 16.131C31.9988 16.0874 32 16.0437 32 16C32 14.5173 30.8027 13.3333 29.3267 13.3333L18.6667 13.3333L18.6667 2.67333C18.6667 2.62963 18.6657 2.58596 18.6637 2.54231C18.6616 2.49866 18.6584 2.45509 18.6542 2.41159C18.65 2.36809 18.6447 2.32473 18.6383 2.28149C18.632 2.23826 18.6246 2.1952 18.6161 2.15233C18.6077 2.10946 18.5982 2.06682 18.5876 2.02442C18.577 1.98201 18.5654 1.93989 18.5528 1.89806C18.5402 1.85622 18.5265 1.81472 18.5119 1.77355C18.4972 1.73239 18.4815 1.69161 18.4649 1.65121C18.4482 1.61082 18.4306 1.57085 18.4119 1.53132C18.3933 1.49179 18.3737 1.45274 18.3532 1.41418C18.3326 1.37561 18.3111 1.33757 18.2887 1.30006C18.2663 1.26255 18.243 1.22561 18.2188 1.18924C18.1945 1.15287 18.1694 1.11713 18.1434 1.08199C18.1174 1.04686 18.0906 1.01238 18.0629 0.978569C18.0352 0.944753 18.0068 0.911634 17.9774 0.879219C17.9482 0.846803 17.9181 0.815125 17.8872 0.784187C17.8563 0.753247 17.8248 0.723084 17.7924 0.6937C17.7601 0.664312 17.727 0.635737 17.6933 0.607972C17.6595 0.580209 17.6251 0.553291 17.59 0.527216C17.555 0.501144 17.5193 0.475944 17.483 0.451622C17.4467 0.4273 17.4098 0.403884 17.3723 0.381375C17.3349 0.358866 17.2969 0.337291 17.2584 0.316644C17.2198 0.296 17.1808 0.276314 17.1414 0.257585C17.1019 0.238856 17.062 0.221107 17.0216 0.204339C16.9813 0.18757 16.9405 0.171803 16.8994 0.157035C16.8582 0.142267 16.8168 0.128518 16.775 0.115787C16.7332 0.103056 16.6911 0.0913588 16.6487 0.080695C16.6063 0.0700312 16.5637 0.0604141 16.5209 0.0518431C16.478 0.0432722 16.435 0.0357581 16.3918 0.0293011C16.3486 0.0228438 16.3052 0.0174512 16.2617 0.0131231C16.2183 0.00879497 16.1747 0.00553669 16.131 0.00334819C16.0874 0.00115969 16.0437 4.36294e-05 16 0C14.5173 0 13.3333 1.19733 13.3333 2.67333L13.3333 13.3333Z" fill="#494A4B"></path>
          </svg>
        </div>
        <input type="file" id="fileInput" accept="image/*" style="display:block; opacity:0; position:absolute; z-index:9999;" />
      </div>
    </div>

    <div class="form-item">
      <div class="bold_text">板块分类</div>
      <div class="typeTab">
        <?php if (!empty($categories)) {
          foreach ($categories as $k => $v) {
        ?>
            <div class="typeTab_item" data-id="<?php echo intval($v->term_id); ?>"><a href="javascript:;"><?php echo esc_attr($v->name); ?></a></div>

        <?php }
        } ?>
        <!--<div class="typeTab_item">前端开发</div>-->
        <!--<div class="typeTab_item typeTab_item_act">Vue.js</div>-->
        <!--<div class="typeTab_item">JavaScript</div>-->
        <!--<div class="typeTab_item">React</div>-->
        <!--<div class="typeTab_item">Node.js</div>-->
        <!--<div class="typeTab_item">CSS</div>-->
      </div>
    </div>


  </div>

  <div class="dialog-footer">
    <button class="btn btn-cancel">取 消</button>
    <button class="btn btn-primary">确 定</button>
  </div>
</div>

<div class="preview-dialog">
  <span class="preview-close">&times;</span>
  <img class="preview-img" src="" alt="图片预览">
</div>
<?php if (is_user_logged_in()) { ?>
  <!-- 手机端发布帖子按钮 -->
  <div class="mb_fbBtn">

    <button style="display: flex;align-items: center;">
      <img src="<?php echo esc_attr(plugins_url('inc/index/assets/img/plan.png', LUNTAN_FILE)); ?>" alt="" />
      发布帖子
    </button>
  </div>
<?php } ?>
</div>
</div>