0

我在 WordPress 中安装了一个自定义图片库,我想在其中一个页脚框中显示它们的数量?我已经尝试了很多,但这是唯一实际显示结果但错误的代码!

<?php
$numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status =       'publish'");
if (0 < $numposts) $numposts = number_format($numposts);

echo 'We have published '.$numposts.' since our launch.';
?>
4

1 回答 1

0

Your query is going to count posts, pages, and custom post types-- anything set to 'publish'. I imagine that that is why you think it is "wrong". Edit the query to restrict it to the post type you want to count. I do not know what post type your "custom image gallery" uses, but here is an example with basic WordPress posts...

$numposts = $wpdb->get_var("
    SELECT COUNT(*) 
    FROM {$wpdb->posts} 
    WHERE post_status = 'publish' 
    AND post_type = 'post'");
于 2012-12-24T16:18:09.803 回答