• Resolved portia79

    (@portia79)


    Hi,

    I’ve managed to slightly adapt some code that I’ve found on the web to display yearly/monthly archive of my custom post type (‘news’):

    2019
      December (3)
      November (1)
    2018
      December (5)
      September (6)

    When you click on any of those it’ll take you to domain.com/news/2019 or domain.com/news/2019/12 archives.

    I’m not having much luck with adding the ul/li markup in between the php code. Could you have a look at the code below and advise? Thank you

    <?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 = 'news' GROUP BY month , year ORDER BY post_date DESC");
    ?>
    <div class="menu">
    <?php
    foreach($months as $month) :
        $year_current = $month->year;
        if ($year_current != $year_prev){
            if ($year_prev != null){?>
    
            <?php } ?>
        <ul>
            <li><a href="<?php bloginfo('url') ?>/news/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a>
            <ul>
        <?php } ?>
        <li><a href="<?php bloginfo('url') ?>/news/<?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)) ?> (<?php echo $month->post_count; ?>)</span></a></li>
        <?php $year_prev = $year_current;
     ?>
            </ul>
            </li> <?php
    endforeach; ?>
    
        </ul>
    </div>
Viewing 5 replies - 1 through 5 (of 5 total)
  • You can read the code of one of these plugins:
    https://www.ads-software.com/plugins/search/yearly+archive/

    Moderator bcworkz

    (@bcworkz)

    Move the first <ul> tag (within the first conditional) out to before the loop even starts to balance the closing </ul> tag after the loop finishes.

    Inside your if ($year_prev != null) conditional output the closing final month </ul> and closing year </li> tags. Move the similar closing tags just before the endforeach to outside the loop to account for the last year where the above conditional will not apply.

    It’ll help if you label the tags with distinguishing class names and subsequent closing tag comments matching the class. And/or use good, strict indent discipline. I think the above corrections will accomplish your desired valid HTML structure. If not, examine the page’s source code and adjust accordingly.

    Thread Starter portia79

    (@portia79)

    Thank you both. I’ve been looking at the code in other plugins but have not made much sense.

    bcworks – I’m not sure I understand your middle paragraph. I’ve done what you recommended in the first paragraph but for the second I’m a bit confused:

    <?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 = 'news' GROUP BY month , year ORDER BY post_date DESC");
    ?>
    <div class="menu">
        <ul>
        <?php
        foreach($months as $month) :
            $year_current = $month->year;
            if ($year_current != $year_prev){
                if ($year_prev != null){
    
                } ?>
                <li><a href="<?php bloginfo('url') ?>/news/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a>
                    <ul>
                    <?php } ?>
                        <li>
                            <a href="<?php bloginfo('url') ?>/news/<?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)) ?> (<?php echo $month->post_count; ?>)</span></a>
                        </li>
                    <?php $year_prev = $year_current; ?>
                    </ul>
                </li> <?php endforeach; ?>
    
        </ul>
    </div>
    Moderator bcworkz

    (@bcworkz)

    This ought to work for you

    <?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 = 'news' GROUP BY month , year ORDER BY post_date DESC");
    ?>
    <div class="menu">
        <ul class="year">
        <?php
        foreach($months as $month) :
            $year_current = $month->year;
            if ($year_current != $year_prev){
                if ($year_prev != null){
                    echo "</ul><!-- .month -->\n\t\t\t</li><!-- .li-year -->\n";
                } ?>
                <li class="li-year"><a href="<?php bloginfo('url') ?>/news/<?php echo $month->year; ?>/"><?php echo $month->year; ?></a>
                    <ul class="month">
                    <?php } ?>
                        <li>
                            <a href="<?php bloginfo('url') ?>/news/<?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)) ?> (<?php echo $month->post_count; ?>)</span></a>
                        </li>
                    <?php $year_prev = $year_current; ?>
               <?php endforeach; ?>
               </ul><!-- .month -->
            </li><!-- .li-year -->
        </ul><!-- .year -->
    </div><!-- .menu -->
    Thread Starter portia79

    (@portia79)

    That’s working perfectly. Thank you.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘custom archive page’ is closed to new replies.