gpt4 book ai didi

php - 如何超链接有帖子的日历日

转载 作者:可可西里 更新时间:2023-10-31 23:00:36 24 4
gpt4 key购买 nike

我有一个用于显示日历的类。

我现在想要的是:显示工作日时,我想超链接在类别事件下发布的所有日期。

我正在考虑在打印日期的函数中添加循环:

function getWeekDays() {
$args = array(
'post_type' => 'post', // the post type
'category_name' => 'events', // name of category
'meta_query' => array(
array(
'key' => 'field_name', // the name of the custom date field
'value' => '20190715', // the date to compare it with
'compare' => '='
)
),
);
$events_dates = new WP_Query( $args );

$weekLength = $this->getWeekLengthByMonth ();
$firstDayOfTheWeek = date ( 'N', strtotime ( $this->currentMonthStart ) );
$weekDays = "";
for($i = 0; $i < $weekLength; $i ++) {
for($j = 1; $j <= 7; $j ++) {
$cellIndex = $i * 7 + $j;
$cellValue = null;
if ($cellIndex == $firstDayOfTheWeek) {
$this->currentDay = 1;
}
if (! empty ( $this->currentDay ) && $this->currentDay <= $this->currentMonthDaysLength) {
$cellValue = $this->currentDay;
$this->currentDay ++;
}
$weekDays .= '<li>' . $cellValue . '</li>';
}
}
return $weekDays;
}

整个类如下所示:

<?php
class calendarWidget{
private $weekDayName = array ("MON","TUE","WED","THU","FRI","SAT","SUN");
private $currentDay = 0;
private $currentMonth = 0;
private $currentYear = 0;
private $currentMonthStart = null;
private $currentMonthDaysLength = null;

function __construct() {

$this->currentYear = date ( "Y", time () );
$this->currentMonth = date ( "m", time () );

if (! empty ( $_POST ['year'] )) {
$this->currentYear = $_POST ['year'];
}
if (! empty ( $_POST ['month'] )) {
$this->currentMonth = $_POST ['month'];
}
$this->currentMonthStart = $this->currentYear . '-' . $this->currentMonth . '-01';
$this->currentMonthDaysLength = date ( 't', strtotime ( $this->currentMonthStart ) );
}

function getCalendarHTML() {
$calendarHTML = '<div id="calendar-outer">';
$calendarHTML .= '<div class="calendar-nav">' . $this->getCalendarNavigation() . '</div>';
$calendarHTML .= '<ul class="week-name-title">' . $this->getWeekDayName () . '</ul>';
$calendarHTML .= '<ul class="week-day-cell">' . $this->getWeekDays () . '</ul>';
$calendarHTML .= '</div>';
return $calendarHTML;
}

function getCalendarNavigation() {
$prevMonthYear = date ( 'm,Y', strtotime ( $this->currentMonthStart. ' -1 Month' ) );
$prevMonthYearArray = explode(",",$prevMonthYear);

$nextMonthYear = date ( 'm,Y', strtotime ( $this->currentMonthStart . ' +1 Month' ) );
$nextMonthYearArray = explode(",",$nextMonthYear);

$navigationHTML = '<div class="prev" data-prev-month="' . $prevMonthYearArray[0] . '" data-prev-year = "' . $prevMonthYearArray[1]. '"><</div>';
$navigationHTML .= '<span id="currentMonth">' . date ( 'M ', strtotime ( $this->currentMonthStart ) ) . '</span>';
$navigationHTML .= '<span contenteditable="true" id="currentYear">'. date ( 'Y', strtotime ( $this->currentMonthStart ) ) . '</span>';
$navigationHTML .= '<div class="next" data-next-month="' . $nextMonthYearArray[0] . '" data-next-year = "' . $nextMonthYearArray[1]. '">></div>';
return $navigationHTML;
}

function getWeekDayName() {
$WeekDayName= '';
foreach ( $this->weekDayName as $dayname ) {
$WeekDayName.= '<li>' . $dayname . '</li>';
}
return $WeekDayName;
}

function getWeekDays() {
$weekLength = $this->getWeekLengthByMonth ();
$firstDayOfTheWeek = date ( 'N', strtotime ( $this->currentMonthStart ) );
$weekDays = "";
for($i = 0; $i < $weekLength; $i ++) {
for($j = 1; $j <= 7; $j ++) {
$cellIndex = $i * 7 + $j;
$cellValue = null;
if ($cellIndex == $firstDayOfTheWeek) {
$this->currentDay = 1;
}
if (! empty ( $this->currentDay ) && $this->currentDay <= $this->currentMonthDaysLength) {
$cellValue = $this->currentDay;
$this->currentDay ++;
}
$weekDays .= '<li>' . $cellValue . '</li>';
}
}
return $weekDays;
}

function getWeekLengthByMonth() {
$weekLength = intval ( $this->currentMonthDaysLength / 7 );
if($this->currentMonthDaysLength % 7 > 0) {
$weekLength++;
}
$monthStartDay= date ( 'N', strtotime ( $this->currentMonthStart) );
$monthEndingDay= date ( 'N', strtotime ( $this->currentYear . '-' . $this->currentMonth . '-' . $this->currentMonthDaysLength) );
if ($monthEndingDay < $monthStartDay) {
$weekLength++;
}

return $weekLength;
}
}

如果当天有事件,当超链接一个月中的任何一天时,我如何运行一个循环来获取事件类别中的所有帖子注意:该循环还需要适用于预定的帖子。

最佳答案

这需要做更多的工作,但它应该能让您入门。

注意:我在测试中包含了原始的 PHPCalendar 样式表。

您将必须创建一个 $arrDaysPosted 索引数组来包含您想要使日历链接可点击的信息。现在,它只存储一天。

        <?php

$style=<<<EOL
<style>
#calendar-outer {
width: 574px;
}

#calendar-outer ul {
margin: 0px;
padding: 0px;
}

#calendar-outer ul li {
margin: 0px;
padding: 0px;
list-style-type: none;
}

.prev {
display: inline-block;
float: left;
cursor: pointer
}

.next {
display: inline-block;
float: right;
cursor: pointer
}

#currentYear:focus {
outline: none;
background: #ff8e8e;
}

div.calendar-nav {
background-color: #ff8e8e;
border-radius: 4px;
text-align: center;
padding: 10px;
color: #FFF;
box-sizing: border-box;
font-weight: bold;
}

#calendar-outer .week-name-title li {
display: inline-block;
padding: 10px 27px;
color: #90918b;
font-size: 0.9em;
font-weight: 600;
}

.week-day-cell li {
display: inline-block;
width: 80px;
height: 80px;
text-align: center;
line-height: 80px;
vertical-align: middle;
background-color: #f6ffc6;
color: #ff8e8e;
border: 1px solid #f1f0f0;
border-radius: 4px;
font-size: 1.2em;
}
#body-overlay {background-color: rgba(0, 0, 0, 0.6);z-index: 999;position: absolute;left: 0;top: 0;width: 100%;height: 100%;display: none;}
#body-overlay div {position:absolute;left:50%;top:50%;margin-top:-32px;margin-left:-32px;}
</style>
EOL;

echo $style;


$args = array(
'post_type' => 'post', // the post type
'category_name' => 'events', // name of category
'meta_query' => array(
array(
'key' => 'field_name', // the name of the custom date field
'value' => '20190715', // the date to compare it with
'compare' => '='
)
),
);
//$args = array( 'post_type' => 'page' );

$query = new WP_Query( $args );
$posts = $query->posts;
$daysPosted = [];

foreach($posts as $post) {
$arrDaysPosted[] = date("n", $post->post_date_gmt);
}
$widget = new calendarWidget($arrDaysPosted);
echo "WIDGET:". $widget->getCalendarHTML();

class calendarWidget{
private $weekDayName = array ("MON","TUE","WED","THU","FRI","SAT","SUN");
private $currentDay = 0;
private $currentMonth = 0;
private $currentYear = 0;
private $currentMonthStart = null;
private $currentMonthDaysLength = null;

function __construct($arrDaysPosted = []) {

$this->arrDaysPosted = $arrDaysPosted;

$this->currentYear = date ( "Y", time () );
$this->currentMonth = date ( "m", time () );

if (! empty ( $_POST ['year'] )) {
$this->currentYear = $_POST ['year'];
}
if (! empty ( $_POST ['month'] )) {
$this->currentMonth = $_POST ['month'];
}
$this->currentMonthStart = $this->currentYear . '-' . $this->currentMonth . '-01';
$this->currentMonthDaysLength = date ( 't', strtotime ( $this->currentMonthStart ) );
}

function getCalendarHTML() {
$calendarHTML = '<div id="calendar-outer">';
$calendarHTML .= '<div class="calendar-nav">' . $this->getCalendarNavigation() . '</div>';
$calendarHTML .= '<ul class="week-name-title">' . $this->getWeekDayName () . '</ul>';
$calendarHTML .= '<ul class="week-day-cell">' . $this->getWeekDays ($this->$arrDaysPosted) . '</ul>';
$calendarHTML .= '</div>';
return $calendarHTML;
}

function getCalendarNavigation() {
$prevMonthYear = date ( 'm,Y', strtotime ( $this->currentMonthStart. ' -1 Month' ) );
$prevMonthYearArray = explode(",",$prevMonthYear);

$nextMonthYear = date ( 'm,Y', strtotime ( $this->currentMonthStart . ' +1 Month' ) );
$nextMonthYearArray = explode(",",$nextMonthYear);

$navigationHTML = '<div class="prev" data-prev-month="' . $prevMonthYearArray[0] . '" data-prev-year = "' . $prevMonthYearArray[1]. '"><</div>';
$navigationHTML .= '<span id="currentMonth">' . date ( 'M ', strtotime ( $this->currentMonthStart ) ) . '</span>';
$navigationHTML .= '<span contenteditable="true" id="currentYear">'. date ( 'Y', strtotime ( $this->currentMonthStart ) ) . '</span>';
$navigationHTML .= '<div class="next" data-next-month="' . $nextMonthYearArray[0] . '" data-next-year = "' . $nextMonthYearArray[1]. '">></div>';
return $navigationHTML;
}

function getWeekDayName() {
$WeekDayName= '';
foreach ( $this->weekDayName as $dayname ) {
$WeekDayName.= '<li>' . $dayname . '</li>';
}
return $WeekDayName;
}

function _new_getWeekDays() {
$old_args = array(
'post_type' => 'post', // the post type
'category_name' => 'events', // name of category
'meta_query' => array(
array(
'key' => 'field_name', // the name of the custom date field
'value' => '20190715', // the date to compare it with
'compare' => '='
)
),
);
$args = array( 'post_type' => 'page' );

$events_dates = new WP_Query( $args );

$weekLength = $this->getWeekLengthByMonth ();
$firstDayOfTheWeek = date ( 'N', strtotime ( $this->currentMonthStart ) );
$weekDays = "";
for($i = 0; $i < $weekLength; $i ++) {
for($j = 1; $j <= 7; $j ++) {
$cellIndex = $i * 7 + $j;
$cellValue = null;
if ($cellIndex == $firstDayOfTheWeek) {
$this->currentDay = 1;
}
if (! empty ( $this->currentDay ) && $this->currentDay <= $this->currentMonthDaysLength) {
$cellValue = $this->currentDay;
$this->currentDay ++;
}
//$weekDays .= '<li>' . $cellValue . '</li>';
}
}
return $weekDays;
}

function getWeekDays() {
$weekLength = $this->getWeekLengthByMonth ();
$firstDayOfTheWeek = date ( 'N', strtotime ( $this->currentMonthStart ) );
$weekDays = "";
for($i = 0; $i < $weekLength; $i ++) {
for($j = 1; $j <= 7; $j ++) {
$cellIndex = $i * 7 + $j;
$cellValue = null;
if ($cellIndex == $firstDayOfTheWeek) {
$this->currentDay = 1;
}
if (! empty ( $this->currentDay ) && $this->currentDay <= $this->currentMonthDaysLength) {
$cellValue = $this->currentDay;

if (in_array($this->currentDay, $this->arrDaysPosted)) {
$weekDays .= '<li>YES:' . $cellValue . '</li>';
} else {
$weekDays .= '<li>'. $this->currentDay .'</li>';
}

$this->currentDay ++;
} else {
$weekDays .= '<li></li>';
}


}
}
return $weekDays;
}

function getWeekLengthByMonth() {
$weekLength = intval ( $this->currentMonthDaysLength / 7 );
if($this->currentMonthDaysLength % 7 > 0) {
$weekLength++;
}
$monthStartDay= date ( 'N', strtotime ( $this->currentMonthStart) );
$monthEndingDay= date ( 'N', strtotime ( $this->currentYear . '-' . $this->currentMonth . '-' . $this->currentMonthDaysLength) );
if ($monthEndingDay < $monthStartDay) {
$weekLength++;
}

return $weekLength;
}
}


?>

关于php - 如何超链接有帖子的日历日,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57039562/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com