如何在我的帖子图中添加日期和首字母?

时间:2018-03-10 作者:Robert Andrews

我一直在构建这个最近的post count仪表板小部件图,或者更准确地说,修改和细化特定于评论的version, 对于帖子。

我真的很高兴。有些可能效率很低,我在不断学习,但我已经成功了。。。

enter image description here

您可以看到,对于每个日期,从特定日期开始,它会从每个日期递增到当前日期,生成一个WP\\u查询以获取该日期的post计数,并将每个添加到一个数组中,从而为图表提供动力。

<?php
/**
 * Plugin Name: Dashboard Widget for Comments
 * Description: Displays a comment-count graph in a dashboard widget. Code inspired by WPMUDev article https://premium.wpmudev.org/blog/adding-custom-widgets-to-the-wordpress-admin-dashboard/
 * Version: 1.0.0
 * Author: Robert Andrews
 */





 function dashboard_widget_display_enqueues( $hook ) {
    if( \'index.php\' != $hook ) {
        return;
    }

    wp_enqueue_style( \'dashboard-widget-styles\', plugins_url( \'\', __FILE__ ) . \'/widgets.css\' );
 }

 add_action( \'admin_enqueue_scripts\', \'dashboard_widget_display_enqueues\' );





 function register_comment_stats_dashboard_widget() {
    wp_add_dashboard_widget(
        \'comment_stats_widget\',
        \'Post Stats\',
        \'comment_stats_dashboard_widget_display\'
    );

 }
 add_action( \'wp_dashboard_setup\', \'register_comment_stats_dashboard_widget\' );












 function comment_stats_dashboard_widget_display() {

   // Initialise array to hold our daily post counts
   $comment_counts = array();

   // Number of days to chart
   $num_days = 14;
   // Start at this time ago
    $date = new DateTime(\'-\'.$num_days.\' days\');

   // For each day of the period
    for (
     $x = 1; // Initial value
     $x <= $num_days; // Iterate this many times
     $x++ // Increment each time
   ) {
     // Go to next day
     $date->modify(\'+1 day\');
     // echo \'Date: \'.$date->format(\'Y-m-d\') . \'<br />\';
     $day_before = new DateTime($date->format(\'Y-m-d\').\' -1 day\');
     // echo \'day_before: \'.$day_before->format(\'Y-m-d\') . \'<br />\';
     $day_after = new DateTime($date->format(\'Y-m-d\').\' +1 day\');
     // echo \'day_after: \'.$day_after->format(\'Y-m-d\') .\'<br />\';

     // WordPress query for posts in time period
     $date_query = array(
                         array(
                             \'after\'=>($day_before->format(\'Y-m-d\')),
                                                        \'before\'=>($day_after->format(\'Y-m-d\')),
                             \'inclusive\' => false,
                             )
                         );
     $args = array(
                     \'post_type\' => \'quote\',
                     \'post_status\'=>\'publish\',
                     // \'category__in\' => (array)$ids,
                     \'date_query\' => $date_query,
                     \'no_found_rows\' => true,
                     \'suppress_filters\' => true,
                     \'fields\'=>\'ids\',
                     \'posts_per_page\'=>-1,
                     \'orderby\' => \'ID\'
                 );
     $query = new WP_Query( $args );

     // echo $query->post_count;

     // Add daily post count to the array
     $comment_counts[] = $query->post_count;


    }









    // eg. $comment_counts = array( 20, 29, 39, 33, 17, 12, 2, 20, 29, 39, 33, 17, 12, 2 );
    $highest_value = max( $comment_counts );
    $data_points = count( $comment_counts );

    $bar_width = 100 / $data_points - 2;

    $total_height = 120;

    ?>

    <div class="comment-stat-bars" style="height:<?php echo $total_height ?>px;">
        <?php
            foreach( $comment_counts as $count ) :
                $count_percentage = $count/$highest_value;
                $bar_height = $total_height * $count_percentage;
                $border_width = $total_height - $bar_height;
        ?>
        <div class="comment-stat-bar" style="height:<?php echo $total_height ?>px; border-top-width:<?php echo $border_width ?>px; width: <?php echo $bar_width ?>%;"></div>
        <?php endforeach ?>
    </div>

    <div class=\'comment-stat-labels\'>
        <?php foreach( $comment_counts as $count ) : ?>
        <div class=\'comment-stat-label\' style=\'width: <?php echo $bar_width ?>%;\'><?php echo $count ?></div>
    <?php endforeach ?>
    </div>

    <div class=\'comment-stat-caption\'>Posts added in the last <?php echo $num_days; ?> days</div>


    <?php
 }


?>
现在我想在图表上的帖子数下面添加两件事。。。

一天的数字(如“10”)和一天的首字母(如“星期六”的“S”)我猜。。。

A.

我想这可能意味着$comment_count 数组不仅包含值,还包含键和值,其中键是相关日期,$date->format(\'Y-m-d\'). 我对这种类型的数组一无所知。这是“关联”数组吗?如何修改。。。

 // Add daily post count to the array
 $comment_counts[] = $query->post_count;
。。。这样做?

B

那么也许,在foreach( $comment_counts as $count ) 通过$comment_count 数组中,我需要将存储的密钥日期同时转换为“10”和“S”?我该怎么做?

我走对了吗?

Edit:

这是成品。。。

enter image description here

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

我想这样说:

A、 使用Unix时间戳作为数组键。

$comment_counts[$date->format(\'U\')] = $query->post_count;
B.带钥匙的回路。

foreach( $comment_counts as $count_key => $count ) :
然后从键中获取日期编号和日期的首字母:

// "d" means with leading zero, use "j" in place of "d" for no leading zero 
$day_number = date("d", $count_key);
$day_initial = substr(date("D" , $count_key), 0, 1);

结束