我如何创建一个快捷代码来显示实际一天的帖子数量?

时间:2021-06-19 作者:Velask

我想创建一个短代码来显示实际日期发布的帖子。

我找到了以下代码:show number of posts posted today我正在尝试,但返回0

function ts_day_f() {
// we get the date for today
$today = getdate();
//we set the variables, i am ignoring sticky posts so they dont get counted
$args = array(
    \'ignore_sticky_posts\' => 1,
    \'posts_per_page\' => -1, //all posts 
    \'date_query\' => array(
        array(
            \'year\'  => $today["year"],
            \'month\' => $today["mon"],
            \'day\'   => $today["mday"],
        ),
    ),
);
//we create the query
$today_posts = new WP_Query( $args );
//the result already has a property with the number of posts returned
$count = $today_posts->post_count;
//show it
return $count;
}

add_shortcode(\'ts_day\',\'ts_day_f\');
有什么想法吗?

1 个回复
最合适的回答,由SO网友:Velask 整理而成

我尝试了一些代码,我想经过几次尝试后,终于可以工作了:

function ts_day_f() {
    $posts= get_posts(array(
            \'numberposts\' => -1,
            \'post_status\' => \'publish\',
            \'orderby\' => \'date\',
            \'order\'   => \'DESC\',
            \'date_query\'    => array(
                \'column\'  => \'post_date\',
                \'after\'     => \'today\',
                \'before\'    => \'tomorrow - 1 second\',
            )
        ));

    if ( is_user_logged_in()) {
        //return count($posts);
        $c = count($posts);

        return "Today: <span style=\\"font-size: 1.5em;\\">" .$c. "</span> posts.";
    }
    else  {
        return $null;
    }
}

add_shortcode(\'ts_day\',\'ts_day_f\');
此外,还具有仅向登录用户显示的功能。

相关推荐

Counting posts and trigger it

我正在试图找出如何为计数柱安装代码。我已经有了网站上帖子的代码。但我想触发它,例如,如果没有帖子,文本应更改为未找到帖子,或者如果只有一篇帖子,文本应为:我们找到了1篇帖子。这是我在函数中编写的代码。phpfunction wpb_total_posts() { $total = wp_count_posts()->publish; echo \'We found\', "<strong>" . $total . "</strong&