WP_Query计数帖子 - 自定义帖子类型

时间:2017-12-22 12:32:46

标签: wordpress count

我如何从自定义帖子中输入数据?

<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>

我如何计算来自&#34; schusslersalz&#34;的条目?在一个数字并显示它。 我有serch并找到了功能:

$count = $loop->post_count;
我怎么用这个?

5 个答案:

答案 0 :(得分:4)

$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;

完整摘录如下:

char

希望我帮助

答案 1 :(得分:0)

试试这个:

$ count_posts = wp_count_posts(&#39; custom_post_type&#39;) - &gt;发布;

答案 2 :(得分:0)

试试这个, [AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)] public sealed class FriendlyError : ExceptionFilterAttribute, IExceptionFilter { private readonly HttpStatusCode StatusCode; public FriendlyError(HttpStatusCode statusCode = HttpStatusCode.InternalServerError) { StatusCode = statusCode; } public override void OnException(ExceptionContext context) { if (context.Exception == null) return; context.HttpContext.Response.StatusCode = (int)StatusCode; context.HttpContext.Response.ContentType = "multipart/form-data;application/json"; context.Result = new JsonResult(CreateErrorResult(context.Exception), new JsonSerializerSettings { Formatting = Formatting.Indented, ReferenceLoopHandling = ReferenceLoopHandling.Ignore, ContractResolver = new CamelCasePropertyNamesContractResolver() }); base.OnException(context); } }

答案 3 :(得分:0)

一种简单的方法来计算包括分页帖子在内的总帖子

<?php  global $wp_query
 $count = $wp_query->found_posts;
echo $count; ?>

答案 4 :(得分:0)

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');