Wordpress: visualizzare nel tema il numero di post pubblicati

Possiamo visualizzare il numero di post pubblicati sul nostro sito tramite la funzione di Wordpress wp_count_posts(). Questa funzione restituisce un oggetto le cui proprietà corrispondono ai vari tipi di post. Per ottenere il numero di post pubblicati dobbiamo usare la proprietà publish. Vediamo come.

Aggiungiamo una funzione al nostro file functions.php del tema corrente:


function count_posts() {
    $count_posts = wp_count_posts();

    $published_posts = $count_posts->publish;

    return '<div class="posts-no"><span>' . $published_posts . ' articoli pubblicati' . '</span></div>';
}

Ora possiamo inserire questa informazione nel nostro template:


<?php echo count_posts();?>

Possiamo a questo punto anche aggiungere degli stili nel CSS del nostro tema:


#branding {
  position: relative;
}

div.posts-no {
  width: 230px;
  position: absolute;
  top: 240px;
  left: 450px;
  -webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  -ms-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;

}

div.posts-no:hover {
  top: 230px;
}

div.posts-no span {
  width: 230px;
  display: block;
  text-align: center;
  color: #fff;
  background: #d84a38;
  text-transform: uppercase;
  padding: 0.5em 0;
  border-radius: 6px;
  -webkit-transition: all 1s ease-in-out;
  -moz-transition: all 1s ease-in-out;
  -o-transition: all 1s ease-in-out;
  -ms-transition: all 1s ease-in-out;
  transition: all 1s ease-in-out;
  -moz-transform: rotate(-5deg);
  -webkit-transform: rotate(-5deg);
  -o-transform: rotate(-5deg);
  -ms-transform: rotate(-5deg);
  transform: rotate(-5deg);
  cursor: pointer;

}

div.posts-no span:hover {
  background: #d34545;
  width: 240px;
  font-size: 1.2em;
  -moz-transform: rotate(0deg);
  -webkit-transform: rotate(0deg);
  -o-transform: rotate(0deg);
  -ms-transform: rotate(0deg);
  transform: rotate(0deg);
}
Torna su