【问题标题】:Wordpress Echo post countWordpress Echo 帖子计数
【发布时间】:2019-01-29 15:47:31
【问题描述】:

我有一个自定义帖子类型,其中包含多个属性, 比如价格 颜色制作模型等。

希望 Wordpress 打印其中的帖子数量。

我当前的代码是这样的

 // Get total number of posts in "vehiclestock" post type
$count_vehiclestock = wp_count_posts('listings');
$total_vehiclestock = $count_vehiclestock->publish;
echo $total_vehiclestock . ' listings. ';

我附上了一些例子来更好地解释我自己。 因为我不知道要搜索什么来回答我自己的问题

【问题讨论】:

    标签: php wordpress slug


    【解决方案1】:

    添加此代码以计算所有帖子或按类别计算帖子

    $args = array(
          'posts_per_page' => -1,
          'post_type'=> 'post',
          'tax_query' => array(
                              array(
                                  'taxonomy' => 'category',
                                  'field'    => 'slug',
                                  'terms'    => 'category_slug'
                              )
                          ),
        ); 
     $wp_query = new WP_Query( $args );
    
     $post_count = $wp_query->post_count;
    

    【讨论】:

    • 我究竟会把我想要的 slug 放在哪里,以便从中获取帖子数量?
    • 这是正确的吗?如果是这样的话。它什么也没显示。甚至没有 0 $args = array( 'posts_per_page' => -1, 'post_type'=> 'listings', 'tax_query' => array( array( 'taxonomy' => 'location', 'field' => 'slug', 'terms' => 'toronto' ) ), ); $wp_query = new WP_Query( $args ); $post_count = $wp_query->post_count;
    • “多伦多”鼻涕虫的任何分配帖子?
    • 是的,只有 1 个帖子
    • 回显像“echo $post_count;”这样的帖子
    猜你喜欢
    • 2012-12-19
    • 1970-01-01
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多