Forum Moderators: coopster
I've got so far, and it seems to be working to some degree, but there are a few things it's doing that I don't know how to fix.
The thing I really have no idea how to fix is when September or August are requested: it returns posts for the whole year, as if $month was set to 00 (see code below).
I don't think this is being caused by wordpress, because if Sep or Aug are requested, the $title is as if the whole year were requested, and $title is set by my script, not wordpress.
Also, if there's anyone familiar with wordpress, is there a way to override the posts-per-page setting in the request below? So far the script only returns 15 posts, but I'd like it to return all the posts available.
<?php
require('wp-blog-header.php');
$currentYear = date(Y);
$currentMonth = date(m);
// Convert the month digits into the month's name
function titlify($switchThis) {
switch ($switchThis) {
case 00 : return "the year "; break;
case 01 : return "January "; break;
case 02 : return "February "; break;
case 03 : return "March "; break;
case 04 : return "April "; break;
case 05 : return "May "; break;
case 06 : return "June "; break;
case 07 : return "July "; break;
case 08 : return "August "; break;
case 09 : return "September "; break;
case 10 : return "October "; break;
case 11 : return "November "; break;
case 12 : return "December "; break;
}
}
// Convert 3-letter into 2 digits
function name2num($switchThis) {
switch ($switchThis) {
case "all" : return 00; break;
case "jan" : return 01; break;
case "feb" : return 02; break;
case "mar" : return 03; break;
case "apr" : return 04; break;
case "may" : return 05; break;
case "jun" : return 06; break;
case "jul" : return 07; break;
case "aug" : return 08; break;
case "sep" : return 09; break;
case "oct" : return 10; break;
case "nov" : return 11; break;
case "dec" : return 12; break;
default : return false;
}
}
// if no GET, set year and month to current
if ($_GET['year']=="" && $_GET['month']=="") {
$year = $currentYear;
$month = $currentMonth;
$title = "Archive for " . titlify($month) . $year; }
// otherwise use GET
else {
$getYear = $_GET['year'];
$getMonth = name2num($_GET['month']); // month is provided as letters, needs to be digits to match currentMonth
}
// if bad year, prompt for good year
if ( $getYear > $currentYear ¦ $getYear < 2004 ¦¦!is_numeric($getYear) ) {
$year = false;
$title = "Invalid year provided"; }
// if no year but yes month, prompt for year
elseif ( empty($getYear) &&!empty($getMonth) ) {
$year = false;
$title = "Invalid year provided"; }
// if yes year but bad month, prompt for good month
elseif (!empty($getYear) && $month == false ) {
$title = "Invalid month provided"; }
// if yes year but no month, show all months for year
elseif (!empty($getYear) && empty($getMonth) ) {
$title = "Archive for the year " . $year;
$query = "year=" . $year;
$date = "d-m-Y"; }
// otherwise everything is fine
else {
$month = $getMonth;
$title = "Archive for " . titlify($month) . $year;
$query = "&year=" . $year . "&monthnum=" . $month;
$date = "jS";
}
function insertForm($month, $year) {?>
<form action="http://example.co.uk/blog/blogarchive.php" method="get">
<label for="month">Month:</label>
<select name="month" id="month">
<option value="jan"<?php if ($month == 01) { echo " selected=\"selected\""; }?>>January</option>
<option value="feb"<?php if ($month == 02) { echo " selected=\"selected\""; }?>>February</option>
<option value="mar"<?php if ($month == 03) { echo " selected=\"selected\""; }?>>March</option>
<option value="apr"<?php if ($month == 04) { echo " selected=\"selected\""; }?>>April</option>
<option value="may"<?php if ($month == 05) { echo " selected=\"selected\""; }?>>May</option>
<option value="jun"<?php if ($month == 06) { echo " selected=\"selected\""; }?>>June</option>
<option value="jul"<?php if ($month == 07) { echo " selected=\"selected\""; }?>>July</option>
<option value="aug"<?php if ($month == 08) { echo " selected=\"selected\""; }?>>August</option>
<option value="sep"<?php if ($month == 09) { echo " selected=\"selected\""; }?>>September</option>
<option value="oct"<?php if ($month == 10) { echo " selected=\"selected\""; }?>>October</option>
<option value="nov"<?php if ($month == 11) { echo " selected=\"selected\""; }?>>November</option>
<option value="dec"<?php if ($month == 12) { echo " selected=\"selected\""; }?>>December</option>
<option value="all"<?php if ($month == 00) { echo " selected=\"selected\""; }?>>Whole Year</option>
</select>
<label for="year">Year:</label>
<select name="year" id="year">
<option value="2006"<?php if ($year == 2006) { echo " selected=\"selected\""; }?>>2006</option>
<option value="2005"<?php if ($year == 2005) { echo " selected=\"selected\""; }?>>2005</option>
<option value="2004"<?php if ($year == 2004) { echo " selected=\"selected\""; }?>>2004</option>
</select>
<input type="submit" value="Go" />
</form>
<?php }
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title><?php echo $title?></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="robots" content="noindex,noarchive,follow" />
<style type="text/css">
<!--
body {
font-family: "Trebuchet MS", sans-serif; }
h1 {
font-family: Georgia, sans-serif;
font-weight: normal; }
-->
</style>
</head>
<body>
<p>Please note this is a work in progress, not the finished item! <a href="http://example.co.uk/blog/blogarchive.php">Reset to blank</a>.</p>
<?php if ($year == false) :?>
<h1><?php echo $title?></h1>
<?php insertForm($month, $year);?>
<p>
Please request a year between 2004 and <?php echo $currentYear;?>
</p>
<?php elseif ($month == false) :?>
<h1><?php echo $title?></h1>
<?php insertForm($month, $year);?>
<p>
Please request a valid month in the form of a two digit number: 01 - 12.
</p>
<?php else :?>
<h1><?php echo $title?></h1>
<?php insertForm($month, $year);
define('WP_USE_THEMES', false);
query_posts($query);
if ( have_posts() ) : // start The Loop
$numberPosts = $wp_query->post_count;
$numberOfPosts = 'numberposts=' . $numberPosts;
$posts = get_posts($numberOfPosts);?>
<h2>Posts</h2>
<p>There are <?php echo $numberPosts;?> posts displayed below<?php if ($numberPosts >= 15) { echo ", but there should be more. Working on it"; }?>.</p>
<p><?php next_posts_link('« Previous Entries')?> • <?php previous_posts_link('Next Entries »')?></p>
<ol>
<?php foreach ($posts as $post) : start_wp();?>
<li>
<strong><?php the_time($date);?>:</strong>
<a href="<?php echo get_permalink()?>" rel="bookmark"><?php the_title();?></a>
</li>
<?php endforeach; // end The Loop?>
</ol>
<p><?php next_posts_link('« Previous Entries')?> • <?php previous_posts_link('Next Entries »')?></p>
<?php else:?>
<p>There are no posts for the month you requested.</p>
<?php endif; // End if have posts?>
<?php endif; // End year/month checking?>
</body>
</html>
<?php
?>
Many thanks, apologies for the large block of code :S
[edited by: coopster at 9:53 pm (utc) on Mar. 13, 2006]
[edit reason] generalized ulr per TOS [webmasterworld.com] [/edit]
EDIT: the problem was caused by storing the months as double digits, I changed them all to single ones and it went away (e.g. september changed from 09 to just 9).