如何使用 WP_Query 统计帖子数?
How to count posts using WP_Query?
以下代码的“if 子句”存在问题。
“Echo”显示正确的数字“3”,但“if count”显示错误的“FOUND one”。
它应该显示“找到很多”,因为计数是 3。
我试过了:
$count_post = $about_preview_query->found_posts;
$posts = get_posts($args);
但“if 子句”仍然显示一个。
请告诉我如何修复代码好吗?
function profile_url(){
$user = wp_get_current_user();
if (!$user->ID) {
return;
}
$args = array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_type' => 'project',
'post_status' => 'publish'
);
$about_preview_query = new WP_Query($args);
$count_post = $about_preview_query->post_count;
if (count($count_post) == 0) {
print "<h1>FOUND None</h1>";
} elseif (count($count_post) == 1) {
print "<h1>FOUND one</h1>";
} else {
print "<h1>FOUND a lot</h1>";
}
echo 'Your Count is: ' . $count_post;
}
谢谢。
你可以试试
function profile_url() {
$user = wp_get_current_user();
if( !$user->ID ){
return;
}
$args = array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_type' => 'project',
'post_status' => 'publish'
);
$about_preview_query = new WP_Query($args);
$count_post = $about_preview_query->found_posts;
if ( $count_post == 0 ) {
print "<h1>FOUND None</h1>"; }
elseif ( $count_post == 1 ) {
print "<h1>FOUND one</h1>";
}else {
print "<h1>FOUND a lot</h1>";
}
echo 'Your Count is: ' . $count_post;
}
以下代码的“if 子句”存在问题。
“Echo”显示正确的数字“3”,但“if count”显示错误的“FOUND one”。
它应该显示“找到很多”,因为计数是 3。
我试过了:
$count_post = $about_preview_query->found_posts;
$posts = get_posts($args);
但“if 子句”仍然显示一个。
请告诉我如何修复代码好吗?
function profile_url(){
$user = wp_get_current_user();
if (!$user->ID) {
return;
}
$args = array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_type' => 'project',
'post_status' => 'publish'
);
$about_preview_query = new WP_Query($args);
$count_post = $about_preview_query->post_count;
if (count($count_post) == 0) {
print "<h1>FOUND None</h1>";
} elseif (count($count_post) == 1) {
print "<h1>FOUND one</h1>";
} else {
print "<h1>FOUND a lot</h1>";
}
echo 'Your Count is: ' . $count_post;
}
谢谢。
你可以试试
function profile_url() {
$user = wp_get_current_user();
if( !$user->ID ){
return;
}
$args = array(
'author' => get_current_user_id(),
'posts_per_page' => -1,
'post_type' => 'project',
'post_status' => 'publish'
);
$about_preview_query = new WP_Query($args);
$count_post = $about_preview_query->found_posts;
if ( $count_post == 0 ) {
print "<h1>FOUND None</h1>"; }
elseif ( $count_post == 1 ) {
print "<h1>FOUND one</h1>";
}else {
print "<h1>FOUND a lot</h1>";
}
echo 'Your Count is: ' . $count_post;
}