0

I've got a custom image gallery in WordPress installed and i want to display in one of the footer boxs how many their are? I've tried lots but this is the only code actually showing a result but its wrong!

<?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 に答える