带有动态内容的Bootstrap手风琴WordPress档案唯一ID

时间:2018-05-09 作者:Deirdre Mc T

​您好,我有一个归档帖子页面,在那里我动态地拉入帖子,然后按年份然后按月排序。我正在尝试添加引导手风琴效果,将一年中的几个月隐藏起来,直到单击为止。

我正在使用echo$year\\u current为每年生成一个唯一的id。但出于某种原因,它仍然不起作用。只是第一年,其他一切都隐藏了起来。我在它上面有一个计数器来应用\'。“在”类中

  <?php $c = 0; ?>
<div id="collapse-<?php echo $year_current; ?>" class="panel-collapse collapse <?php if( $c == 1 ) echo \'in\'; ?>" role="tabpanel" aria-labelledby="heading-<?php echo $year_current; ?>">
任何帮助都将不胜感激!这是我的代码:

  <?php
/*
Template Name: Archives
*/
get_header(); ?>

<div id="mid_yellow">
  <p>Archives</p>
</div>
  <div id="mid_yellow">
  </div>
    <p ></p>
    <?php $c = 0; ?>
    <?php
    global $wpdb;

    $limit = 0;
    $year_prev = null;
    $months = $wpdb->get_results("SELECT DISTINCT MONTH( post_date ) AS month , YEAR( post_date ) AS year, COUNT( id ) as post_count FROM $wpdb->posts WHERE post_status = \'publish\' and post_date <= now( ) and post_type = \'post\' GROUP BY month , year ORDER BY post_date DESC");

    foreach($months as $month) :

        $year_current = $month->year; $c++;

        if ($year_current != $year_prev)
        {
            if($year_current != date(\'Y\'))
            {
            ?>
                </ul>
            <?php
            }
            ?>

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
  <div class="panel panel-default">
    <div class="panel-heading" role="tab" id="heading-<?php echo $year_current; ?>">
      <h4 class="panel-title">
            <a data-toggle="collapse" data-parent="#accordion" aria-controls="collapse-<?php echo $year_current; ?>" href="#collapse-<?php echo $year_current; ?>/"><?php echo $month->year; ?></a>
        </h4>
    </div>

    <div id="collapse-<?php echo $year_current; ?>" class="panel-collapse collapse <?php if( $c == 1 ) echo \'in\'; ?>" role="tabpanel" aria-labelledby="heading-<?php echo $year_current; ?>">
      <ul class=\'list-group\'>
        <?php
        }
        ?>

            <li class=\'list-group-item\'>
                <a href="<?php bloginfo(\'url\') ?>/<?php echo $month->year; ?>/<?php echo date("m", mktime(0, 0, 0, $month->month, 1, $month->year)) ?>"><span class="archive-month"><?php echo date_i18n("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span></a>
            </li>

        <?php
        $year_prev = $year_current;

    endforeach;
    ?>
      </ul>
    </div>
  </div>
</div>

<?php get_footer(); ?>

1 个回复
SO网友:Jacob Peattie

the_ID() 是针对帖子id的,因此只有在为单个帖子添加id属性时才相关。

出于您的目的,您可以使用任何您想要的东西,只要它对年份是唯一的,而且年份本身也是唯一的,只需使用它即可:$year_current.

例如:

<div id="collapse-<?php echo $year_current; ?>" class="panel-collapse collapse" role="tabpanel" aria-labelledby="heading-<?php echo $year_current; ?>">

结束

相关推荐

Display an archives name

我想显示当前存档文件的名称,因此对于下面的url,我可以这样做<h1><?php echo \"answer\" ?></h1> 输出如下内容<h1>News</h1> 或者更好<h1>News: supplierName</h1> http://www.site.com/suppliers/news/?supplierstax=supplierName