需要显示从实际日期7帖子

时间:2019-11-29 作者:Lestra

我有特定的类别,在哪些方面我有很多帖子。每个职位都从2019年10月21日开始。

我有以下代码以正确的顺序显示帖子:

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;
    }
}
现在我只需要显示实际日期后的7篇帖子。

我试过这个

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;
    }
}
但它不起作用,因为我有很多帖子是在同一天发表的。

我需要显示从第一个代码中计算的实际日期的7个帖子。泰

1 个回复
SO网友:mems

Try 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;
}

相关推荐

如何将Single-*.php放在特定的文件夹中?

我正在组织和清理代码,以使其更易于维护。事实证明,我的网站有很多自定义帖子类型,所以我有大约15个单发-***。我的网站根目录下的php文件。为了使树视图更清晰,我希望所有这些文件都列在一个特定的“单个”文件夹中。显然,Wordpress只识别单个-***。位于主题根目录的php文件。有办法解决这个问题吗?提前感谢您的帮助。