Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Misal post dengan type video di kategori work
- Cara 1:
- <?php $posts = get_posts('post_type=videos&category=4');
- $count = count($posts);
- echo $count;
- ?>
- Cara 2 (pakai WP_Query >> http://codex.wordpress.org/Class_Reference/WP_Query)
- <?php
- $args = array(
- 'cat' => 4,
- 'post_type' => 'videos'
- );
- $the_query = new WP_Query( $args );
- echo $the_query->found_posts;
- ?>
- Cara 3: (doc>> https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters)
- <?php
- $the_query = new WP_Query( array(
- 'post_type' => 'CUSTOM_POST_TYPE',
- 'tax_query' => array(
- array(
- 'taxonomy' => 'CUSTOM_TAXONOMY',
- 'field' => 'id',
- 'terms' => TERM_ID
- )
- )
- ) );
- $count = $the_query->found_posts;
- ?>
- ===================
- CARA LAIN
- ===================
- KETEMU DI >> https://wordpress.org/support/topic/display-number-of-posts-per-category/
- Pasang di function:
- function number_postpercat($idcat) {
- global $wpdb;
- $query = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat";
- $num = $wpdb->get_col($query);
- echo $num[0];
- }
- Pasang di theme:
- <?php number_postpercat (4); ?>
- =======================================
- KODE LIST DAFTAR CATEGORI dan COUNT POS
- =======================================
- Pasang di function:
- function number_postpercat($idcat) {
- global $wpdb;
- $query = "SELECT count FROM $wpdb->term_taxonomy WHERE term_id = $idcat";
- $num = $wpdb->get_col($query);
- echo $num[0];
- }
- Pasang di theme:
- foreach (get_categories(array('hide_empty'=>false)) as $category)
- {
- echo '<li><a href="' . get_bloginfo('wpurl') .
- '/category/' . $category->category_nicename . '/">' .
- $category->cat_name . '</a> // ' . $category->count .
- 'posts</li>';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement