WordPress customized calendar next and previous ye

2019-09-14 19:53发布

问题:

I have a calendar that I created in WordPress. There is a next and previous month link at the top. They navigate fine through the current year, but when the year changes to either the next or previous year, I get a 404 error.

The link with each month looks like this (January 2014 for example): mysiteurl.com/calendar-grid/?year=2014&month=1

I have tried just typing in the correct url with and without the / before the ?year and that doesn't work.

This is how I am getting the data:

if($_GET['month'] == ''){
    $currmonth = date('n');
    $thisyear = date('Y');
}
else {
    $currmonth = $_GET['month'];
    $thisyear = $_GET['year'];
}
$thismonth = paddNum($currmonth);
$firstday = $thisyear.$thismonth.'01';
$lastday = date('Ymt', strtotime($firstday));
$numdays = $lastday - $firstday + 1;
$firstDOW = date('N', strtotime($thisyear.'/'.$thismonth.'/01')) + 1;
if($firstDOW == 8) $firstDOW = 1;
$nextmonth = $thismonth + 1;
$prevmonth = $thismonth - 1;
$nextyear = $thisyear;
$prevyear = $thisyear;
if($nextmonth == 13){
    $nextmonth = 1;
    $nextyear = $thisyear + 1;
}
elseif($prevmonth == 0){
    $prevmonth = 12;
    $prevyear = $thisyear - 1;
}
$args = array(
    'post_type' => 'event',
    'meta_query' => array(
        array(
            'key'       => 'event_date',
            'compare'   => 'BETWEEN',
            'value'     => array($firstday, $lastday)
        )
    ),
    'posts_per_page' => -1
);
$posts = get_posts($args);

This is how the links are created:

<div class="month-head-text">
    <a class="month-prev" href="<?php echo get_home_url(); ?>/calendar-grid?year=<?php echo $prevyear; ?>&month=<?php echo $prevmonth; ?>">
        <img src="<?php echo get_template_directory_uri(); ?>/images/prev-month.png" />
    </a>
    <div class="month-name"><?php echo translateMonth($thismonth); ?></div>
    <a class="month-next" href="<?php echo get_home_url(); ?>/calendar-grid?year=<?php echo $nextyear; ?>&month=<?php echo $nextmonth; ?>">
        <img src="<?php echo get_template_directory_uri(); ?>/images/next-month.png" />
    </a>
    <a class="close-cal" href="<?php echo get_home_url(); ?>/calendar"><img src="<?php echo get_template_directory_uri(); ?>/images/close.png" /></a>
</div>

here is the link for December 2014: http://houseof.mspaceap.com/calendar-grid/?year=2014&month=12

回答1:

I was using reserved terms for POST and GET: http://codex.wordpress.org/Function_Reference/register_taxonomy#Reserved_Terms

Once I changed the variables to passyear and passget everything started working.