使用php在WP循环中生成结束div

时间:2014-10-26 21:53:50

标签: php wordpress

我是php和Wordpress的新手,我想要完成的是按月+年列出我的所有帖子。月份+年份显示为链接,单击此链接可切换日期。我的css代码看起来像这样并且正常工作(由jquery完成切换功能)。

<div class="monat"> <!-- month div -->
    <a href="#" title="Oktober 2014" class="zeigeTage">Oktober 2014</a> <!-- Toggle Button -->
         <div class="tage"> <!-- toggle div - shown when toggle button clicked -->
            <a href="#" title="#" class="tag">12</a> <!-- day of month -->
            <a href="#" title="#" class="tag">20</a> <!-- day of month -->
            <a href="#" title="#" class="tag">22</a> <!-- day of month -->
            <a href="#" title="#" class="tag">23</a> <!-- day of month -->
    </div>    <!-- end toggle div -->     
</div>  <!-- end month div --> 

这就是我迄今为止在php中所取得的成就。问题是,我需要在结尾处使用2个div(对于类monat和class tage),我不知道如何在php代码中实现它们...

<?php

$prev_month = '';
$prev_year = '';

while(have_posts())

{
  the_post();
  if(get_the_time('F') != $prev_month || get_the_time('Y') != $prev_year)

  {
  echo '<div class="monat"><a href="#" title="#" class="zeigeTage">'.get_the_time('F Y').'</a><div class="tage">';
  }

?>

<a href="<?php the_permalink(); ?>" title="#" class="tag"><?php the_time('j'); ?></a>

<?php
   $prev_month = get_the_time('F');
   $prev_year = get_the_time('Y');
}
?>

感谢您的帮助, 安德烈亚斯

2 个答案:

答案 0 :(得分:0)

非常感谢,哈克! 像魅力一样工作!

以下是编辑过的代码:

<?php
                        $prev_month = '';
                        $prev_year = '';
                        $divs_opened = false;

                        query_posts( 'category_name=wurst' );

                        while(have_posts()) { ## Loop Start

                            the_post();
                            $month_changed = (get_the_time('F') != $prev_month) || (get_the_time('Y') != $prev_year);
                            if ($month_changed) {
                                if ($divs_opened) {
                                    echo "</div></div>";
                                }
                                $divs_opened = true;

                                {
                                echo '<div class="monat"><a href="#" title="#" class="zeigeTage">'.get_the_time('F Y').'</a><div class="tage">';
                                }
                            }
                    ?>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" class="tag"><?php the_time('j'); ?></a>
                    <?php
                            $prev_month = get_the_time('F');
                            $prev_year = get_the_time('Y');
                        } ## Loop End

                        if ($divs_opened) {
                            echo "</div></div>";
                        }
                    ?> 

答案 1 :(得分:-1)

您已经了解了如何检测月变化事件:

$month_changed = (get_the_time('F') != $prev_month) 
                 || (get_the_time('Y') != $prev_year);
if ($month_changed) {
    ...
}

你输出(这里我已经放置了......)div的开头。但你仍然错过了什么时候关闭它们。

首先,您只需在打开任何内容时放置结束div标签。您可以像使用月份检查一样执行此操作,只需使用另一个变量:

$prev_month = '';
$prev_year = '';
$divs_opened = false; ## third variable

while (have_posts()) {
    ...

所以现在当你第一次打开div时,将$divs_opened变量设置为true

$month_changed = (get_the_time('F') != $prev_month) 
                 || (get_the_time('Y') != $prev_year);
if ($month_changed) {
    $divs_opened = true;
    ...
}

如果他们已经打开了你需要关闭div,你也需要检查它:

$month_changed = (get_the_time('F') != $prev_month) 
                 || (get_the_time('Y') != $prev_year);
if ($month_changed) {
    if ($divs_opened) {
        echo "</div></div>";
    }
    $divs_opened = true;
    ...
}

所以现在这已经差不多完成了,但是还有一个地方你需要关闭div(如果打开):那是在循环之后:

$prev_month = '';
$prev_year = '';
$divs_opened = false; ## third variable

while (have_posts()) {
    ...
}

if ($divs_opened) {
    echo "</div></div>";
}

应该这样做。我希望它能为您提供指示,您可以使用类似的方法来检测结束div的月份变化。

相关问题