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
110 stars 29 forks source link

Display webcomic thumbnail/first image #325

Closed misternek closed 6 years ago

misternek commented 6 years ago

I'm trying to develop a custom Webcomic theme for my own website, and I'm at the point where I'm creating the archive.php page:

<div id="gallery">
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink(); ?>"><?php webcomic_media(); ?></a>

<?php endwhile; ?><?php else : ?>
<?php endif; ?>
</div>

It's working fine until I make a webcomic post with more than one piece of media. When I do that it display all of the post's media on the page, when I only want one thumbnail image. I've tried the default Wordpress thumbnail tag and that doesn't return any images. I've also looked at the wiki but it's not entirely clear to me how to configure the tag to just show one image from the post.

Sorry if this sounds like a noob question, I only have the most basic knowledge of PHP.

mgsisk commented 6 years ago

Hey @misternek, that's great that you're doing a custom Webcomic theme! I'd love to see it when you're done.

The documentation for webcomic_media() is a little obtuse, but this should get you the result you're looking for:

<?php webcomic_media( 'full', null, [ 'length' => 1 ] ); ?>

The first argument is the size of the media you want to display, the second is the post you want to display media for (null here so that we default to the loop's current post), and the last is an array with the length key set to 1, which is what should actually limit how many media items are displayed.

misternek commented 6 years ago

Hi @mgsisk! Unfortunately this code isn't fixing the problem. The archive page is still showing all of my gallery post's media:

galleryissue

Here is my entire archive.php file if it helps. I also tried resetting the permalinks and renaming it to archive-webcomic1.php and got the same results.

<?php get_header(); ?>

<div id="gallery">
<?php if (have_posts()) : ?><?php while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink(); ?>"><?php webcomic_media( 'full', null, [ 'length' => 1 ] ); ?></a>

<?php endwhile; ?><?php else : ?>
<!-- 404 text will go here -->
<?php endif; ?>
</div>

<?php get_footer(); ?>
mgsisk commented 6 years ago

Thanks @misternek; totally my fault. The length argument requires the offset argument, as well, but of course I forgot that. Try this one:

<?php webcomic_media( 'full', null, [ 'offset' => 0, 'length' => 1 ] ); ?>

The offset and length arguments work together; here, they're telling Webcomic "of all the media this post has, start at the first media item (offset => 0) and just show one (length => 1).

misternek commented 6 years ago

This worked perfectly, thank you @mgsisk!