Files
vij/src/calend-block.php

104 lines
3.2 KiB
PHP
Raw Normal View History

2021-10-31 13:01:31 +03:00
<?php
function calend_block($month, $year, $script=false){
$args = array(
'numberposts' => -1,
'category' => 61, // 61 - мероприятия
'orderby' => 'date',
'order' => 'ASC',
'date_query' => array(
array(
'after' => array( // после этой даты
'year' => $year,
'month' => $month,
'day' => 1,
),
'before' => array( // до этой даты
'year' => $year,
'month' => $month,
'day' => 31,
),
// 'inclusive'=> true
)
),
'post_type' => 'post',
'post_status' => 'publish, future',
'suppress_filters' => true, // подавление работы фильтров изменения SQL запроса
);
$posts = get_posts($args);
$events = [];
foreach ($posts as $post){
$key = date('d.m.Y', strtotime($post->post_date));
$events[$key]['text'] = $post->post_title;
$events[$key]['link'] = get_permalink($post->ID);
}?>
<div id="event-calendar">
<?=Calendar::getMonth(date($month), date($year), $events);?>
</div>
<?php if ($script){?>
<script>
var calend_link_base = "<?=get_site_url()?>/wp-content/themes/vij/src/eventcalendar.php";
var calend_curr_year = "<?=$year?>";
var calend_curr_month = "<?=$month?>";
function calend_prev_month(){
load_calendar_month(type = 'prev');
}
function calend_next_month(){
load_calendar_month(type = 'next');
}
function load_calendar_month(type = 'next'){
if (type == 'next'){
if (calend_curr_month == 12 ){
calend_curr_month = 1;
calend_curr_year = parseInt(calend_curr_year) + 1;
}
else {
calend_curr_month = parseInt(calend_curr_month) + 1;
}
}
else if (type == 'prev'){
if (calend_curr_month == 1 ){
calend_curr_month = 12;
calend_curr_year = parseInt(calend_curr_year) - 1;
}
else {
calend_curr_month = parseInt(calend_curr_month) - 1;
}
}
$currlink = calend_link_base + '?year=' + calend_curr_year + '&month=' + calend_curr_month;
fetch($currlink).then(function (response) {
return response.text();
}).then(function (html) {
document.getElementById('event-calendar').innerHTML = html;
}).catch(function (err) {
console.warn('Something went wrong.', err);
});
}
</script>
<?}
}