WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. thebluebox
    Member
    Posted 1 year 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>
          <?php
            $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){?>
                </ul>
                <?php } ?>
              <h3><?php echo $month->year; ?></h3>
              <ul class="archive-list">
              <?php } ?>
              <li>
                <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>
                </a>
              </li>
            <?php $year_prev = $year_current;
            endforeach; ?>
            </ul>

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

    Any help would be wonderful!

  2. thebluebox
    Member
    Posted 1 year 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