¿Quieres mostrar los recuentos de entradas, páginas y comentarios a través de una plantilla? Aquí tienes una selección de snippets que te ayudarán a hacerlo. Este fragmento te permite mostrar el número de entradas, páginas y entradas personalizadas publicadas y guardadas como borrador. También puedes mostrar el número de comentarios en estas listas: Pendientes, aprobados, spam, basura y total.
Instrucciones:
Todo lo que tiene que hacer es añadir estos fragmentos a su archivo de plantilla.
Fragmento nº 1
<?php // count posts $n_post = wp_count_posts(); $n_post_publish = $n_post->publish; $n_post_drafts = $n_post->draft; // count pages $n_page = wp_count_posts('page'); $n_page_publish = $n_page->publish; $n_page_drafts = $n_page->draft; // count custom post types $n_cpt = wp_count_posts('CUSTOM_POST_TYPE_NAME'); $n_cpt_publish = $n_cpt->publish; $n_cpt_drafts = $n_cpt->draft; // count comments $n_comments = wp_count_comments(); $n_comments_moderated = $n_comments->moderated; $n_comments_approved = $n_comments->approved; $n_comments_spam = $n_comments->spam; $n_comments_trash = $n_comments->trash; $n_comments_total = $n_comments->total_comments; ?>
Fragmento #2
<ul> <!-- posts --> <li>Post publish: <? echo $n_post_publish; ?></li> <li>Post drafts: <? echo $n_post_drafts; ?></li> <!-- pages --> <li>Page publish: <? echo $n_page_publish; ?></li> <li>Page drafts: <? echo $n_page_drafts; ?></li> <!-- custom post types --> <li>Custom post publish: <? echo $n_cpt_publish; ?></li> <li>Custom post drafts: <? echo $n_cpt_drafts; ?></li> <!-- comments --> <li>Coments moderated: <? echo $n_comments_moderated; ?></li> <li>Coments approved: <? echo $n_comments_approved; ?></li> <li>Coments spam: <? echo $n_comments_spam; ?></li> <li>Coments trash: <? echo $n_comments_trash; ?></li> <li>Coments total: <? echo $n_comments_total; ?></li> </ul>
Nota: Si es la primera vez que añade fragmentos de código en WordPress, consulte nuestra guía sobre cómo copiar / pegar correctamente fragmentos de código en WordPress, para no romper accidentalmente su sitio.
Si le gustó este fragmento de código, por favor considere revisar nuestros otros artículos en el sitio como: Los mejores constructores de páginas de WordPress (Comparados).
Hello, I have written the first snippet in code snippet. But then, how can I display the amounts?
Hi, love the code,it works great. One question though, how do I get this count to display on the front end? Specifically for a variety of different Custom Post Types.
Hi Kevin, I am from Turkey so sorry my bad English language. I have a download a web site and I want to see Custom field count. For exapmple; read/download: 100/37 what can I do?
thanks its working
May I congratulate you on your intelligent work.You have won me over and gained a new fan. Compared to most articles on the web this is a joy to read, I am overjoyed that I came upon your work. I promise to be linking to this on all my blogs.
Wow thanks, this is very nice to hear. I’m glad that I could post content that is of such value to people that they post great comments like this. Thanks again
Hi,your code for showing the number of pages doesn’t work,you should change pages parameter in line 7 to page in order to work
Good catch on the typo thanks, updated the post.