I've been using the code below to count items in a directory and then, by using a plugin, add it to a WordPress page.
$dir = '/PATH TO DIRECTORY/';
$filecount = 0;
$d = dir($dir);
while ($f = $d->read()) {
if(($f!= ".") && ($f!= "..")) {
if(!is_dir($f)) $filecount++;
}
}
echo '(',$filecount,')';
Wondering whether I can make a shortcode from it, I've pre-pended it with
function item_count() {
And appended it with:
}
add_shortcode('count', 'item_count');
But the echo is triggering a 'headers already sent' error.
I've since learned that shortcodes should 'return' rather than 'echo', but haven't been able to figure-out what I need to do to use this as a shortcode.