大佬教程收集整理的这篇文章主要介绍了WordPress。如何在taxonomy.php中获得多个自定义post_type总帖子数?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的 wordpress v5.7 taxonomy.PHP
模板中,我使用以下代码来获取自定义的 post_type
总帖子数。 (https://stackoverflow.com/a/66751447/3725816 处建议的代码)。
$term = get_querIEd_object();
$args = array(
'post_type' => 'your-post-type-name','post_status' => 'publish',// get only publish posts
'posts_per_page' => -1,// get all posts
'tax_query' => array(
'relation' => 'AND',array(
'taxonomy' => 'your-taxonomy-name','fIEld' => 'term_ID','terms' => $term->term_ID
)
)
);
$AllPostByTerms = new WP_query( $args );
echo $AllpostByTerms->post_count;
现在我的 wordpress 中有多个(许多)自定义 post_type。
不是为 taxonomy.PHP
模板中的每个 post_type 重复相同的代码,有没有一种方法可以在单个函数/查询中获取每个自定义 post_type
的帖子总数?
$args = array(
'post_type' => ['post-type-name1','post-type-name2','post-type-name3'],'post_status' => 'publish',// get only publish posts
'posts_per_page' => -1,// get all posts
'tax_query' => array(
'relation' => 'AND',array(
'taxonomy' => 'your-taxonomy-name','field' => 'term_id','terms' => $term->term_id
)
)
);
您可以简单地在帖子类型参数中传递所有自定义帖子类型的数组
,所以我只是偶然发现了你的问题,不确定你是否仍然有兴趣看到答案,但我会添加我的答案以供将来参考,以防万一有人需要它。
因此,您可以通过多种方式实现您的目标!但是,您在问题中没有提到某些事情!
例如,在计算“总帖子数”时,您没有提到您指的是哪个帖子状态!
您的问题中还不清楚的另一件事是您有兴趣计算的“帖子类型”!
例如,除了你的 custom-post-type-1、custom-post-type-2、custom-post-type-3,wordpress 还有它自己的“内置”帖子类型,例如:>
我会给你我的答案,你可以根据需要随意定制!
由于您没有提到您要查找的具体“帖子类型”,我假设您只对计算您的 custom-post-type-1
、custom-post-type-2
和 {{ 1}} 等。因此,我们将使用名为 custom-post-type-3
的 wordpress 函数来动态获取我们感兴趣的所有自定义帖子类型。此外,我将假设,您有兴趣数get_post_types
。为此,我将使用一个名为 all of the post statuses
的 wordpress 函数。
get_post_typesDocs
wp_count_postsDocs
现在使用上述函数,让我们创建我们的神奇函数,它将返回 wp_count_posts
,其中包含每个自定义帖子类型的每个状态的计数!
不需要 WP_QUERY!
a multidimentional associative array
这将返回一个看起来像这样的 function your_them_counter_post_types()
{
$args = array(
'public' => true,'_builtin' => false // This will leave out built-in post types (i.e Post,Page,Attachment,Nav_menu_item,Revision)
);
$output = 'names';
$operator = 'and';
$post_types = get_post_types($args,$output,$operator);
$counts_array = array();
if ($post_types) {
if (!is_array($post_types)) {
$post_types = (array)$post_types;
} // just to make sure our 'foreach' doesn't throw an error!
foreach ($post_types as $post_type) {
$current_post_type = wp_count_posts($post_type);
$counts_array[$post_type] = array(
'published' => intval($current_post_type->publish),'draft' => intval($current_post_type->draft),'pending' => intval($current_post_type->pending),'future' => intval($current_post_type->future),'private' => intval($current_post_type->private),'trash' => intval($current_post_type->trash)
);
}
}
return $counts_array;
}
:
multidimentional associative array
现在我们有了一个神奇的函数,我们需要做的就是调用它并循环遍历返回的数组,如下所示:
$counts_array
(
[custom-post-type-1] => Array
(
[published] => 10,[draft] => 2,[pending] => 3,[future] => 0,[private] => 0,[trash] => 1
),[custom-post-type-2] => Array
(
[published] => 20,[draft] => 5,[pending] => 4,[trash] => 2
),[custom-post-type-3] => Array
(
[published] => 30,[draft] => 7,[pending] => 1,[trash] => 3
)
)
这将返回您所要求的内容!
$posts_counts_array = your_them_counter_post_types();
foreach ($posts_counts_array as $post_type_name => $post_count_array) {
printf(
__('For %s: ','Your-Theme-text-domain'),$post_type_name
);
echo "<pre>";
foreach ($post_count_array as $post_status_name => $post_counts) {
printf(
_n(
'There is %1$s %2$s post.','There are %1$s %2$s posts.',$post_counts,'Your-Theme-text-domain'
),$post_status_name
);
echo "<br>";
}
echo "</pre>";
}
注意:
我还使用以下函数进行翻译并将值打印到页面上:__
Docs_n
Docsprintf
Docs
以上是大佬教程为你收集整理的WordPress。如何在taxonomy.php中获得多个自定义post_type总帖子数?全部内容,希望文章能够帮你解决WordPress。如何在taxonomy.php中获得多个自定义post_type总帖子数?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。