我有一个显示日历的小部件,在其中一个功能中,我有一个名为getWeekDays的方法,该方法显示一周中的所有日子。
在该方法中,我运行sql查询以显示日历中所选当月的帖子列表,然后将所有帖子日期存储在数组中。
function getWeekDays() {
global $wpdb;
$postDates = [];
$this->searchDate = preg_replace('/\W\w+\s*(\W*)$/', '$1', "{$this->currentMonthStart}");
$posts = $wpdb->get_results('SELECT `post_date` FROM `wp_posts` WHERE `post_date` LIKE "%' . $this->searchDate . '%"');
foreach ($posts as $post) {
$postDates[] = $post->post_date;
}
$args = array(
'post_type' => 'post', // the post type
'category_name' => 'events', // name of category
);
$the_query = 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>'; // here I print all the week days
}
}
return $weekDays;
}
现在,我想要以下示例: 如果3号有帖子,则数字3必须具有href,我应该可以访问当天的所有帖子详细信息。
我尝试在用于显示日期的for循环内添加wp_query,但显示错误。
我该如何实现?
编辑:
以下是我尝试过的:
$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 ++;
}
while($query->have_posts()) : $query->the_post();
$weekDays .= '<li>' . $cellValue . '</li>';
endwhile; wp_reset_query();
}
}
return $weekDays;
查看页面时,我看到以下错误:
: Uncaught Error: Call to a member function have_posts()