Support » Fixing WordPress » Display the name of the Month on the monthly archi

  • Folks,
    I’m using clean uri’s (with the default wp mod_rewrite rules), so my “monthly” and “yearly” archives show up like: mybloghome/archives/2004/02/ and mybloghome/archives/2004/ respectively.
    How I can display the name of the month (or the year) once at the top of the page that these uri’s generate, ie. outside “the loop”?
    I’ve tried both the single_month_title and the_date functions, but neither seem to display anything…

Viewing 5 replies - 1 through 5 (of 5 total)
  • Humm.. Good luck.
    I believe you could retrieve the URI with PHP and extract the Month or Year from it, then translate it in full english, but that requires quite a lot of programming…
    Actually maybe not that much. But I cannot help in this department.

    I bet there’s an easier way, using WP API and internal variables, but I do not know enough about WP yet…
    But actually, the PHP programming to display these info is not so heavy, depending on what you want…
    whether you use mod_rewrite or not, all archive parameters are passed to the page in the $_REQUEST[] variable.
    The following bits of code should display respectively the day, month and year (if any) of the category being displayed:
    <?php echo $_REQUEST['day']; ?>
    <?php echo date("F", strtotime($_REQUEST['monthnum'])); ?>
    <?php echo $_REQUEST['year']; ?>
    However, the previous bits are of little use if you have a common template for month and year archives (since you won’t know when to display either date format).
    This slightly more complex block would solve the problem:
    $day_tmp = "D F G, Y";
    $month_tmp = "F Y";
    $year_tmp = "Y";
    if (! empty($_REQUEST['day']))
    echo date($day_tmp, strtotime( $_REQUEST['year'] . "-" . $_REQUEST['monthnum'] . "-" . $_REQUEST['day']) );
    elseif (! empty($_REQUEST['monthnum']))
    echo date($month_tmp, strtotime( $_REQUEST['year'] . "-" . $_REQUEST['monthnum'] . "-01") );
    elseif (! empty($_REQUEST['year']))
    echo date($year_tmp, strtotime( $_REQUEST['year'] . "-01-01") );

    With this one bit, you do not have to worry about any check… it will only display date info when they are available.
    All you have to do is change the template format for each archive type (the $day_tmp etc. at the beginning). They use PHP’s date() syntax, which is pretty easy and completely documented here:
    Hope this help.

    Ooops, just realized one of the template string above is wrong, it should be:
    $day_tmp = "D F j, Y";

    Glad you found a solution (definitely more elegant than my php hack)…
    however, you said:

    Of course, I wouldn’t need to do this if I wasn’t using mod_rewrite, as the $m, $y and $p variables would be available.

    As I said, this is not completely true: you can easily access month, year and day through $_REQUEST[‘monthnum’], $_REQUEST[‘year’] and $_REQUEST[‘day’]
    Actually, that’s all the mod_rewrite does: turns your slash-separated strings into POST arguments…

    Hey, I got bstovold’s code to work for my purposes just using built in variables: $single, $cat, $m, $author. Any page that doesn’t have one of those values is the front page (at least according to my formula).

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Display the name of the Month on the monthly archi’ is closed to new replies.