在这个脚本中,我需要将$ count变量替换为wordpress数据库中的帖子计数:
<?php
$state_query = $wpdb->get_results("
SELECT meta_value AS state, COUNT(post_id) AS count
FROM {$wpdb->postmeta} WHERE meta_key = 'state'
GROUP BY state ORDER BY count DESC");
$count = 50; //here i need to count the number of meta value of each post
$max = 100;
$scale = 1.0;
if ( !empty($max) ) { $percent = ($count * 100) / $max; }
else { $percent = 0; }
?>
<?php if ($state_query) {
echo '<div class="cf">'; ?>
<?php foreach ($state_query as $st)
echo ($percent * $scale);
?>
<?php echo '</div>'; } ?>
答案 0 :(得分:0)
尝试
$count = count($state_query);
答案 1 :(得分:0)
您可以像这样使用num_rows
$count = $wpdb->num_rows;
答案 2 :(得分:0)
将$ wpdb声明为全局并使用它来执行返回PHP对象的SQL查询语句
试试这会对你有所帮助。
$state_query = $wpdb->get_results("
SELECT meta_value AS state, COUNT(post_id) AS count
FROM {$wpdb->postmeta} WHERE meta_key = 'state'
GROUP BY state ORDER BY count DESC", OBJECT );
if($wpdb->num_rows)
{
foreach ($state_query as $result)
{
$count = $result->count;
$max = 100;
$scale = 1.0;
}
}else{
$count = 50; //here i need to count the number of meta value of each post
$max = 100;
$scale = 1.0;
}