admin管理员组文章数量:1406010
I have specific category, in what I have a lot of posts. Every post starts from, for example 21 october 2019.
I have this code to show posts in the correct sequence:
class GoroskopController
{
static function get_data($parent_id, $number, $heading, $heading_color = 'dark')
{
$loop = new \WP_Query(GoroskopModule::loop_args($parent_id, $number));
if ($loop->have_posts()) :
echo '<div class="goroskop-block mb30">';
echo '<div class="block-heading-color-small -' . $heading_color . ' font-sans">' . $heading . '</div>';
while ($loop->have_posts()) : $loop->the_post();
$this_date = self::this_gor(get_the_title());
include "templates/block.php";
endwhile;
wp_reset_query();
echo ($parent_id == 156) ? '<div class="view-more font-sans"><a href="' . get_category_link($parent_id) . '">' . esc_html__('Смотреть все', 'grimple_core') . '</a></div>' : '';
echo '</div>';
endif;
}
static function now_date()
{
$m = [
'january' => '01',
'february' => '02',
'march' => '03',
'april' => '04',
'may' => '05',
'june' => '06',
'jule' => '07',
'august' => '08',
'september' => '09',
'october' => '10',
'november' => '11',
'december' => '12'
];
return $m;
}
static function this_gor($title)
{
$gor_date = explode(' ', $title);
$m = self::now_date();
$this_date = date('d.m.Y');
$day = (mb_strlen($gor_date[0],'UTF-8') == 1) ? '0' . $gor_date[0] : $gor_date[0];
$month = $gor_date[1];
$year = $gor_date[2];
foreach ($m as $key => $value) {
if ($month == $key)
$month = $value;
}
$sep = '.';
$gor_out_date = $day . $sep . $month . $sep . $year;
$this_gor = ($this_date == $gor_out_date) ? true : '';
return $this_gor;
}
}
and now I need to show only 7 posts from actual date.
I tried this
class GoroskopModule
{
static function loop_args($parent_id, $number)
{
$args = [
'cat' => $parent_id,
'showposts' => '7',
'orderby' => 'ID',
'order' => 'asc',
'date_query' => array('after' => date('d.m.Y', strtotime('-2 days')) )
];
return $args;
}
}
but it not work because I have a lot of posts what published in same day.
I need to show 7 posts from actual date calculated in first code. ty
I have specific category, in what I have a lot of posts. Every post starts from, for example 21 october 2019.
I have this code to show posts in the correct sequence:
class GoroskopController
{
static function get_data($parent_id, $number, $heading, $heading_color = 'dark')
{
$loop = new \WP_Query(GoroskopModule::loop_args($parent_id, $number));
if ($loop->have_posts()) :
echo '<div class="goroskop-block mb30">';
echo '<div class="block-heading-color-small -' . $heading_color . ' font-sans">' . $heading . '</div>';
while ($loop->have_posts()) : $loop->the_post();
$this_date = self::this_gor(get_the_title());
include "templates/block.php";
endwhile;
wp_reset_query();
echo ($parent_id == 156) ? '<div class="view-more font-sans"><a href="' . get_category_link($parent_id) . '">' . esc_html__('Смотреть все', 'grimple_core') . '</a></div>' : '';
echo '</div>';
endif;
}
static function now_date()
{
$m = [
'january' => '01',
'february' => '02',
'march' => '03',
'april' => '04',
'may' => '05',
'june' => '06',
'jule' => '07',
'august' => '08',
'september' => '09',
'october' => '10',
'november' => '11',
'december' => '12'
];
return $m;
}
static function this_gor($title)
{
$gor_date = explode(' ', $title);
$m = self::now_date();
$this_date = date('d.m.Y');
$day = (mb_strlen($gor_date[0],'UTF-8') == 1) ? '0' . $gor_date[0] : $gor_date[0];
$month = $gor_date[1];
$year = $gor_date[2];
foreach ($m as $key => $value) {
if ($month == $key)
$month = $value;
}
$sep = '.';
$gor_out_date = $day . $sep . $month . $sep . $year;
$this_gor = ($this_date == $gor_out_date) ? true : '';
return $this_gor;
}
}
and now I need to show only 7 posts from actual date.
I tried this
class GoroskopModule
{
static function loop_args($parent_id, $number)
{
$args = [
'cat' => $parent_id,
'showposts' => '7',
'orderby' => 'ID',
'order' => 'asc',
'date_query' => array('after' => date('d.m.Y', strtotime('-2 days')) )
];
return $args;
}
}
but it not work because I have a lot of posts what published in same day.
I need to show 7 posts from actual date calculated in first code. ty
Share Improve this question edited Nov 29, 2019 at 9:48 Chetan Vaghela 2,4084 gold badges10 silver badges16 bronze badges asked Nov 29, 2019 at 7:02 LestraLestra 1 2 |1 Answer
Reset to default 0Try this
static function loop_args($parent_id, $number) {
$today = getdate();
$args = [
'cat' => $parent_id,
'showposts' => '7',
'orderby' => 'ID',
'order' => 'asc',
'date_query' => array(
array(
'year' => $today["year"],
'month' => $today["mon"],
'day' => $today["mday"],
),
),
];
return $args;
}
本文标签: phpNeed to show 7 posts from actual date
版权声明:本文标题:php - Need to show 7 posts from actual date 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744956578a2634390.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
"posts_per_page" => 7,
look in the documentation for more details – Kaperto Commented Nov 29, 2019 at 7:15