简体   繁体   English

如何获得一个WordPress类别的总职位数?

[英]how to get the number of total posts in a wordpress category?

I want to show the number of posts in a category and replace that with "99" in my code. 我想显示一个类别中的帖子数,并在我的代码中将其替换为“ 99”。 this is my codes : 这是我的代码:

 <?php wp_nav_menu( array( 'theme_location' => 'filmview-menu', 'walker' => new wp_materialize_navwalker() ) ); ?> 

 $('ul.menu-odd').each(function () { if ($(this).children().length > 4){ $(this).addClass("two_cl"); $(this).find('li > a').append('<span class="badge menu_num" data-badge-caption="(99)"></span>'); } }); 

$category = get_category($id);
$count = $category->category_count;
echo $count;

get the category by ID and save it in a variable. 通过ID获取类别并将其保存在变量中。 This variable is now that category object and it has a lot of associated information stored in it. 现在,此变量就是该类别对象,并且其中存储了许多关联信息。 Within the object array there will be the post count. 在对象数组中将有发布计数。 It is called by using $category->category_count and saved as a variable that you can then echo anywhere after that. 通过使用$ category-> category_count调用它,并将其另存为变量,然后可以在其后的任何地方回显。

You can achieve this by simple WP_Query. 您可以通过简单的WP_Query来实现。 And using tax_query for filtering based on custom taxonomy. 并使用tax_query基于自定义分类法进行过滤。

$args = array(
    'post_type' => 'YOUR_POST_TYPE',
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'YOUR_CUSTOM_TAXONOMY',
            'field'    => 'slug', // Can also put 'term_id'
            'terms'    => 'bob',  // Custom taxonomy ID
        ),
    ),
);
$query = new WP_Query( $args );

And you get the total count by post_count variable, 然后通过post_count变量获得总数,

<?php echo $query-> post_count; ?>

For more details on tax_query please check this out, 有关tax_query的更多详细信息,请查看此信息,

https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Hope this one helps. 希望这对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM