mgsisk / webcomic

Comic publishing power for the web. Turn your WordPress-powered site into a comic publishing platform with Webcomic.
http://wordpress.org/plugins/webcomic
GNU General Public License v2.0
109 stars 29 forks source link

Count number of webcomics per collection #133

Closed cleverchicken closed 11 years ago

cleverchicken commented 11 years ago

I've been setting up my site and using the template tags to set up a webcomic directory (I have multiple webcomics). I realized that if a collection was empty, my template would still try to pull the data of that collection and the latest webcomic thumbnail. I wanted to setup an if statement to determine if the collection had any webcomics at all and if it doesn't (webcomic count = 0) then don't display anything.

Problem is I can't seem to find a way to count the number of webcomics in a collection. Am I overlooking a template tag or another solution to this? Any ideas would be appreciated!

Great plugin by the way!

cleverchicken commented 11 years ago

Unless there is a better way, I'll just use: if( WebcomicTag::webcomic_dropdown_collections(array( 'collection' => $collection['id'])) { // do stuff } It seems to work.

mgsisk commented 11 years ago

There are several ways to do this (including the one you posted, though I'm not sure I'd actually use that one). If we're talking about a template that uses a custom loop, you should be able to check whether or not there are any posts with the have_posts() function, like:

$webocmics = new WP_Query( array( 'post_type' => 'webcomic42' ) );
if ( $webcomics->have_posts() ) :

/* only do this if webcomic42 has published webcomics */

endif;

Alternatively, the WordPress function wp_count_posts might be exactly what you need and should work in almost any situation. You can use it like:

$webcomics = wp_count_posts( 'webcomic42' ); // or you could use get_webcomic_collection() to grab whatever the current collection is

if ( $webcomics->publish ) :

/* only do stuff if there are published webcomics in webcomic42 */

endif;
cleverchicken commented 11 years ago

Perfect! Your second solution works beautifully. Thanks a ton. Again, excellent plugin, sir.

mgsisk commented 11 years ago

Thanks!