Wordpress and Webdesign Forum go41 » WordPress Code Snippets

  1. hi, i would like to show some posts from about one year ago in the sidebar of my WordPress blog.
    Can you help me?

    Posted 2 years ago #
  2. @cat:
    to get the posts of the current month and current year you find this code in WordPress Codex:

    <?php $current_month = date('m'); ?>
    <?php $current_year = date('Y'); ?>
    <?php query_posts("cat=22&year=$current_year&monthnum=$current_month&order=ASC"); ?>
    <!-- put your loop here -->

    With this code and a modified current_year variable you can get wordpress query the posts for the same month you have now but one year earlier.

    I use this code to retrieve one year old posts:

    <h3>one Year ago</h3>
    <?php $current_month = date('m'); ?>
    <?php $lastyear = date('Y', mktime(0, 0, 0, date("m") , date("d"), date("Y") - 1)); ?>
    <?php $oldposts = get_posts("numberposts=4&year=$lastyear&monthnum=$current_month&order=DESC");
    	foreach($oldposts as $post) : setup_postdata($post); ?>
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>

    here the current year is taken and with the help of the php function mktime one year is taken off. Same thing would work for any number of years or also months ago.

    Posted 2 years ago #
  3. Another piece of code to get 4 random old post listed in your sidebar.php of wordpress is this:

    <h3>Oldies but Goodies</h3>
    <ul>
    <?php global $wp_query; $rndposts = get_posts('numberposts=4&offset=15&orderby=rand()');
     foreach($rndposts as $post) : setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>
    </ul>

    put this code anywhere in your sidebar and find random old post titles with link in there.

    Posted 2 years ago #

RSS feed for this topic

Reply

You must log in to post.

Join us! or log in (lost password?):