[英]WP_Query count posts - custom Post Type
我如何从自定义帖子类型中计算条目?
<ul class="test">
<?php $args = array( 'post_type' => 'schusslersalz', 'posts_per_page' => 30, 'orderby' => 'rand' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<li>';
the_title('<h3>', '</h3>');
the_content();
echo '</li>';
endwhile; ?>
</ul>
我如何计算“schusslersalz”中的条目并显示它。 我已经搜索并找到了功能:
$count = $loop->post_count;
我怎么用这个?
wp_count_posts
函数有参数$type
用于统计帖子类型,如果你想统计schusslersalz
数量,你应该使用这个参数
一个片段
$count_posts = wp_count_posts( 'schusslersalz' )->publish;
echo $count_posts;
完整片段如下:
$args = array(
'post_type' => 'schusslersalz'
);
$the_query = new WP_Query( $args );
echo $the_query->found_posts;
希望我有所帮助
试试这个, $loop->found_posts;
计算总帖子的简单方法,包括分页帖子
<?php global $wp_query
$count = $wp_query->found_posts;
echo $count; ?>
尝试这个:
$count_posts = wp_count_posts( 'custom_post_type' )->publish;
function wpb_total_posts() { $today = date('Y-m-d'); $args = array( 'post_type' => 'grants', 'meta_query' => array( array( 'key' => 'application_deadline', 'value' => $today, 'type' => 'DATE', 'compare' => '>' )),); $the_query = new WP_Query( $args ); return $the_query->found_posts; } add_shortcode('total_posts','wpb_total_posts');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.