✅ Notizie, temi, plugin WEB e WordPress. Qui condividiamo suggerimenti e le migliori soluzioni per siti web.

Come mostrare le statistiche del grafico del numero mensile di post in WordPress?

21

L’URL: https://helloacm.com/archives-of-pagesposts/ mostra l’elenco dei post storici e ho aggiunto una bella figura seguente che traccia il numero di post per ogni mese.

grafico-post-al-mese-wordpress-php-js

Puoi vedere un’attività approssimativa negli ultimi 12 mesi. Sì, a volte diventiamo pigri durante tutto l’anno. Questo post mostra come creare una pagina del genere per ottenere i post e il codice seguente può essere aggiunto nella stessa pagina per tracciare il grafico.

Dobbiamo includere una libreria javascript Chart di terze parti che può essere trovata su: https://helloacm.com/js/chart.js

  echo '<script language="Javascript" src="<a class="vglnk" target="_blank" href="https://helloacm.com/js/chart.js" rel="nofollow"><span>https</span><span>://</span><span>helloacm</span><span>.</span><span>com</span><span>/</span><span>js</span><span>/</span><span>chart</span><span>.</span><span>js</span></a>"></script>'."n";
  global $wpdb; // <a class="vglnk" target="_blank" href="http://helloacm.com" rel="nofollow"><span>helloacm</span><span>.</span><span>com</span></a>
  
  // count the posts number group by each month
  $query = "
      select 
        concat(year(`post_date`), '-', month(`post_date`)) as `month`, 
        count(1) as `cnt` 
      from 
        `wp_posts` 
      where 
        (post_type='page' or post_type='post') and 
        `post_status` = 'publish' 
      group by 
        `month` 
      order by 
        `post_date` desc
      limit
         12
";  // change this to include more months. the default is 12 months in the past.
      
  $result = array_reverse($wpdb->get_results($query));// reverse the data to get a normal logic flow
  if ($result) {
?>
 
<div id='ReportBarChartContainerMonthly'>
    <div id='ReportBarChartComponentMonthly' style='margin: 15px;'>
        <canvas id="myChartMonthly" style='max-width:100%;height:auto'></canvas>
    </div>
</div>
    
<script language="Javascript">
    var data = {
        labels: [
        
            <?php
    foreach ($result as $month) {
      echo '"'.$month->month.'", ';  
    }        
            ?>
        ],
        datasets: [
            {
                label: "Number of Posts",
                fillColor: "rgba(200,200,250,0.7)",
                strokeColor: "rgba(150,150,220,1)",
                pointColor: "rgba(220,220,220,1)",
                pointStrokeColor: "#fff",
                pointHighlightFill: "#fff",
                pointHighlightStroke: "rgba(220,220,220,1)",
                data: [
            <?php
    foreach ($result as $month) {
      echo '"'.$month->cnt.'", ';  
    }        
            ?>
                ]
            }
        ]
    };
 
    var ctx = document.getElementById("myChartMonthly").getContext("2d");
    var myNewChart = new Chart(ctx).Line(data, {
        bezierCurve: true
    });
</script>
<?php
  }

Salva il file modello e svuota la cache, la trama verrà quindi resa viva.

TODO: (1) parametro impostato su 12 mesi per impostazione predefinita, modificalo di conseguenza nell’istruzione SQL (2) funzione di inversione dell’array in php invertire i dati di output in modo che l’asse corrispondente al tempo (da sinistra a destra). (3) cambia il nome della tabella di WordPress se non inizia con il prefisso wp.

Fonte di registrazione: helloacm.com

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More