WordPress.org

Ready to get started?Download WordPress

Forums

Hacking the Calendar (6 posts)

  1. BlaenkDenum
    Member
    Posted 6 years ago #

    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.

  2. Kafkaesqui

    Posted 6 years ago #

    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:

    <?php
    global $m, $post;
    $m = get_the_time('Ym');
    get_calendar();
    ?>
  3. BlaenkDenum
    Member
    Posted 6 years ago #

    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: http://codex.wordpress.org/Template_Tags/get_the_time 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()) ?

  4. BlaenkDenum
    Member
    Posted 6 years ago #

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

  5. Kafkaesqui

    Posted 6 years ago #

    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:

    /?m=200711

    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()).

  6. BlaenkDenum
    Member
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic