Support » Plugins » Hacking the Calendar

  • Hey guys I would like to hack my calendar widget so that it displays the month/year of the currently viewed post. So say I view a post from last November, it’d show November 2006 for the calendar. After some searching I found that the function get_calendar is in template-functions-general.php. I figure I have to replace the $year, $month, and $weekday. Possibly the other globals that are imported into that function’s scope.

    The reason I haven’t advanced was because I don’t know how to get the information for the currently viewed post. After getting something like the Post ID I could simply query the database.

    I think I read somewhere that if one’s permalinks are in a certain format then it would automatically work, however I don’t want to change to that permalink structure for certain reasons.

    I would appreciate any input on this, thanks.

Viewing 5 replies - 1 through 5 (of 5 total)
  • That’s right, that is how get_calendar() works if the archival date (month and year) appears in the permalink structure. But we can trick get_calendar() here:

    global $m, $post;
    $m = get_the_time('Ym');

    Thanks! So I should just do that in widgets.php? Or do I have to still modify template-functions-general.php?

    What I think you meant was to do:

    global $m, $post;
    $m = get_the_time(‘Ym’);

    Before the get_calendar call. Oh, but wouldn’t the time that is retrieved be the current time? Ah, according to this: it doesn’t, it gets the post for the current time. Alright! So I’m guessing I would just put that above the get_calendar() call in widgets.php. Oh, and then there’s the case of whether they’re even viewing a post right, how can I check for that? if (is_post()) ?

    Oh, but is it possible to do is_post() from widgets.php? Wouldn’t it always come up as false?

    First, I did exactly what I meant to do. ;)

    Yes, my code would go into either your template or the widget plugin’s file. The goal was to avoid editing WordPress’ source.

    get_calendar() looks for an archive-related global var to choose between present and archive date. $m is a global query var set through:


    in the url. By forcing a value for $m using get_the_time(), we’re convincing get_calendar() to use the archival date. And get_the_time() receives the date from the $post object, which will hold the ‘current’ individual post, or the latest in any query page you’re on. There should be no need for conditional testing (I think you mean is_single()).

    By meant was, what I understood from what you said, sorry. Thanks it works flawlessly, I appreciate the help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hacking the Calendar’ is closed to new replies.