Question

I have a question and I have not yet found a solution. I have an archive in my blog which looks like:

May[2014]
  -Title 1 article
May[2014]
  -Title 2 article
April[2014]
  -Title 3 article

But I want if there are one or more articles publishees in the same month to show like this:

May[2014]
 -Title 1 article
 -Title 2 article
May[2014]
 Title 3 article

My code:

<?php if ($archive): $_month = '0000-00'; ?>
                <?php foreach($archive as $a):?>
                    <div class="archive">
                        <div class="month">
                        <?php if (date('Y-m', strtotime($a['date'])) !== $_month) :?>
                            + <?php echo date('M',strtotime($a['date']))?> [<?php echo date("Y",strtotime($a['date']))?>]
                                <div class="month-article last-article-content">
                                    <span class="most-popular-articles"><img src="<?php echo config_item('base_url');?>assets/images/arrow.png">&nbsp&nbsp<a href="<?php echo base_url('materials/'.$a['id'].'/'.make_permalink($a['title'])).'.html'; ?>"><?php echo ($a['title']) ?></a></span>
                                </div>
                        <?php endif; ?>
                        <?php $_month = date('Y-m',strtotime($a['date'])) ?>
                        </div>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>
Was it helpful?

Solution

$a['date']+1 won't work as you can imagine.

You have to create a variable which contains the previous date, assuming your articles are ordered by date.

<?php 
$_month = '0000-00';
foreach ($articles as $a) : 

    if (date('Y-m', strtotime($a['date'])) !== $_month) {
        echo date('M',strtotime($a['date']));
    }
    ...

    $_month = date('Y-m', strtotime($a['date']));

endforeach; 
?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top