The Support Forums will be in read-only mode for a scheduled maintenance window on 01 September 2016 14:00 UTC - 20:00 UTC. More information.

How can I display custom archive code in a widget? (3 posts)

  1. thebluebox
    Posted 3 years ago #

    I'm using the code from this site, to display my news archives in the sidebar of my custom theme. It looks great but it is just pasted directly into my sidebar.php file and I would love to have it as a widget.

    So how can I convert the following code into a widget?

    <h1><div id="archives-title"><h3>News Archives</h3></div>
            $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;
              if ($year_current != $year_prev){
                if ($year_prev != null){?>
                <?php } ?>
              <h3><?php echo $month->year; ?></h3>
              <ul class="archive-list">
              <?php } ?>
                <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("F", mktime(0, 0, 0, $month->month, 1, $month->year)) ?></span>
                  <span class="archive-count">(<?php echo $month->post_count; ?>)</span>
            <?php $year_prev = $year_current;
            endforeach; ?>

    I've been looking for days, but can't find out how to do it.

    Any help would be wonderful!

  2. Mark Ratledge
    Forum Moderator
    Posted 3 years ago #

  3. thebluebox
    Posted 3 years ago #

    Sadly, that plugin does not work with this code.

    I have now managed to make a custom widget. However, I can't get the above code to work in it. Is there a way to get 'echo' to execute php?

Topic Closed

This topic has been closed to new replies.

About this Topic