• hi, i have created my own calendar plugin but whatever i do i cannot get it to go to january 2025. i end up with a message:

    This page doesn’t seem to exist.It looks like the link pointing here was faulty. Maybe try searching?

    although the navigation bar has the correct address showing “mysite.local/events-calendar/?month=1&year=2025”

    from November 2024, i can easily go to December 2024 by pressing next month button, but after that its error.

    can anyone help?

    this is my code:

    // Shortcode for calendar month view

    add_shortcode('calendar_month_view', 'display_calendar_month_view');

    function display_calendar_month_view() {

    ? ? ob_start();

    ? ? // Get the current month and year from URL parameters or default to the current date

    ? ? $current_month = isset($_GET['month']) ? (int)$_GET['month'] : date('n');

    ? ? $current_year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');

    ? ? // Adjust month and year for navigation

    ? ? if ($current_month < 1) {

    ? ? ? ? $current_month = 12;

    ? ? ? ? $current_year--;

    ? ? } elseif ($current_month > 12) {

    ? ? ? ? $current_month = 1;

    ? ? ? ? $current_year++;

    ? ? }

    ? ? // Calculate the number of days and starting weekday of the month

    ? ? $days_in_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);

    ? ? $first_day_of_month = strtotime("$current_year-$current_month-01");

    ? ? $first_weekday = date('w', $first_day_of_month);

    ? ? // Days of the week header

    ? ? $days_of_week = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];

    ? ? echo '<div class="calendar-navigation">';

    ? ? // Previous Month link

    ? ? $prev_month = $current_month - 1;

    ? ? $prev_year = $current_year;

    ? ? if ($prev_month < 1) {

    ? ? ? ? $prev_month = 12;

    ? ? ? ? $prev_year--;

    ? ? }

    ? ? echo '<a href="?month=' . $prev_month . '&year=' . $prev_year . '">Previous Month</a>';

    ? ? // Current Month and Year label

    ? ? echo '<span>' . date('F Y', $first_day_of_month) . '</span>';

    ? ? // Next Month link

    ? ? $next_month = $current_month + 1;

    ? ? $next_year = $current_year;

    ? ? if ($next_month > 12) {

    ? ? ? ? $next_month = 1;

    ? ? ? ? $next_year++;

    ? ? }

    ? ? echo '<a href="?month=' . $next_month . '&year=' . $next_year . '">Next Month</a>';

    ? ? echo '</div>';

    ? ? echo '<div class="calendar-grid">';

    ? ? // Print days of the week

    ? ? foreach ($days_of_week as $day) {

    ? ? ? ? echo '<div class="calendar-day-header">' . $day . '</div>';

    ? ? }

    ? ? // Create empty boxes for the first week

    ? ? for ($i = 0; $i < $first_weekday; $i++) {

    ? ? ? ? echo '<div class="calendar-day empty"></div>';

    ? ? }

    ? ? // Loop through each day of the month

    ? ? for ($day = 1; $day <= $days_in_month; $day++) {

    ? ? ? ? $date_string = "$current_year-$current_month-$day";

    ? ? ? ? $posts = get_posts(array(

    ? ? ? ? ? ? 'post_type' => 'post',

    ? ? ? ? ? ? 'meta_query' => array(

    ? ? ? ? ? ? ? ? array(

    ? ? ? ? ? ? ? ? ? ? 'key' => 'event_start_date',

    ? ? ? ? ? ? ? ? ? ? 'value' => $date_string,

    ? ? ? ? ? ? ? ? ? ? 'compare' => '<=',

    ? ? ? ? ? ? ? ? ? ? 'type' => 'DATE'

    ? ? ? ? ? ? ? ? ),

    ? ? ? ? ? ? ? ? array(

    ? ? ? ? ? ? ? ? ? ? 'key' => 'event_end_date',

    ? ? ? ? ? ? ? ? ? ? 'value' => $date_string,

    ? ? ? ? ? ? ? ? ? ? 'compare' => '>=',

    ? ? ? ? ? ? ? ? ? ? 'type' => 'DATE'

    ? ? ? ? ? ? ? ? )

    ? ? ? ? ? ? ),

    ? ? ? ? ));

    ? ? ? ? echo '<div class="calendar-day">';

    ? ? ? ? echo '<strong>' . $day . '</strong>';

    ? ? ? ? if ($posts) {

    ? ? ? ? ? ? // Define an array to hold colors for each unique event

    ? ? ? ? ? ? $event_colors = [];

    ? ? ? ? ? ? $colors = ['#FFB3B3', '#B3CFFF', '#B3FFB3', '#FFEBB3', '#D7B3FF', '#FFB3D7', '#B3FFDA', '#FFFAB3', '#B3B3FF', '#FFB3B3']; // Add more pastel colors if needed

    ? ? ? ? ? ? foreach ($posts as $post) {

    ? ? ? ? ? ? ? ? setup_postdata($post);

    ? ? ? ? ? ? ? ? $title = get_the_title($post);

    ? ? ? ? ? ? ? ? $truncated_title = mb_substr($title, 0, 20);

    ? ? ? ? ? ? ? ? $post_link = get_permalink($post->ID);

    ? ? ? ? ? ? ? ? $post_content = get_the_content(null, false, $post); // Get the full post content

    ? ? ? ? ? ? ? ? $image_from_content = get_first_image_from_content($post_content); // Get the first image

    ? ? ? ? ? ? ? ? // Use post ID as the unique key for color assignment

    ? ? ? ? ? ? ? ? $unique_key = $post->ID;

    ? ? ? ? ? ? ? ? // Assign a unique color based on the hash of the post ID

    ? ? ? ? ? ? ? ? if (!isset($event_colors[$unique_key])) {

    ? ? ? ? ? ? ? ? ? ? $index = $unique_key % count($colors); // Ensure unique color assignment based on post ID

    ? ? ? ? ? ? ? ? ? ? $event_colors[$unique_key] = $colors[$index];

    ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? $event_color = $event_colors[$unique_key]; // Get the assigned color

    ? ? ? ? ? ? ? ? echo '<div class="event" style="background-color: ' . esc_attr($event_color) . '; cursor: pointer;">';

    ? ? ? ? ? ? ? ? echo '<a href="' . esc_url($post_link) . '" style="text-decoration: none; color: inherit;">';

    ? ? ? ? ? ? ? ? echo '<span>' . esc_html($truncated_title) . '</span>';

    ? ? ? ? ? ? ? ? echo '</a>';

    ? ? ? ? ? ? ? ? echo '<div class="tooltip" style="z-index: 9999;">';

    ? ? ? ? ? ? ? ? echo '<a href="' . esc_url($post_link) . '" style="text-decoration: none; color: inherit;">';

    ? ? ? ? ? ? ? ? if ($image_from_content) {

    ? ? ? ? ? ? ? ? ? ? echo '<img src="' . esc_url($image_from_content) . '" style="width: 100%; height: auto; object-fit: contain;">'; // Use object-fit

    ? ? ? ? ? ? ? ? }

    ? ? ? ? ? ? ? ? echo '<strong>' . esc_html($title) . '</strong>';

    ? ? ? ? ? ? ? ? echo '</a>';

    ? ? ? ? ? ? ? ? echo '</div>';

    ? ? ? ? ? ? ? ? echo '</div>';

    ? ? ? ? ? ? }

    ? ? ? ? ? ? wp_reset_postdata();

    ? ? ? ? }

    ? ? ? ? echo '</div>';

    ? ? }

    ? ? echo '</div>';

    ? ? return ob_get_clean();

    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    “year” is a reserved query var, you cannot use it for your custom query because WP will try to match the requested year for the published year of the events-calendar page itself. Use a different query var for your calendar year that’s not a reserved word, maybe “event_year”?

    While “year” is reserved, “month” is not. No need to change “month”, but I recommend doing so anyway. IMO we should avoid using common words for query vars since they could become a conflict in the future. I typically place “bcw” (from my display name) in front of everything to avoid possible conflicts. My chosen query vars for something like this would be “bcw-year” and “bcw-month”.

    puts moderator hat on…
    Please refrain from posting large amounts of code in these forums. Short snippets of a dozen or so lines are OK. For lengthier code, please use a public repository like pastebin.com or gist.github.com and just post the resulting link here.

    Thread Starter kooshool

    (@kooshool)

    wow man, thanks a lot. you saved me from arguing with these ai assistants. I wrote it again in html and used the classic editor to get it up, but now i can easily switch back to what i intended. thank you thank you thank you.

    btw sorry for pasting large amounts of code, I am very new to this space and mostly clueless about what im doing.

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.