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

Transcripts internal loop resets to main loop instead of custom loop #184

Closed GaryTomato closed 10 years ago

GaryTomato commented 10 years ago

Hi Mike,

My setup (also mentioned in issue #176) is quite simple, I want the last posted webcomic on the main page just like as it looks on single.php. I copied single.php and I use a static page as a front page. I included a custom loop using a WP_Query object so it only displays one post and only webcomic type posts. It works well and displays comments related to the post. My issue started when I started using transcripts. The code in transcripts.php has a custom loop and at the end it properly calls wp_reset_postdata. But the query is reset to the "page" type query instead of my "post" type so instead of the comments for the post I get the comments for the page I set as front page. I have tried cloning wp_query and also my custom WP_Query object to no avail. If I switch the transcript line and the comments line so the comments are invoked first, the comments are properly displayed for the post.

My only workaround currently is to reinitialize my custom loop right after I invoked the transcripts and then print the comments. (As I only have one entry, it's ok, but I think it's a really bad coding habit which is going to break something soon. And also I'm doing database queries twice.)

This might be something related to how WordPress works, but I'm not sure and I don't have the experience. Can you please give me some directions on how to use the query after transcripts? Shall I make sure I only use the main loop and not a custom one?

To move away from custom loops I tried pre_get_posts but I think it's overkill and I couldn't identify properly when it should modify the queue and when should it leave it alone. (It seems to be called quite a lot of times on a page load in different areas. For example the menu.) I tried overwriting values in the global $wp_query variable which didn't work and I tried query_posts to change the queue but all seem to have some kind of issues. (This latter didn't work out because the page was also identified as an archive page so it gave me the big webcomic picture and then a thumbnail before printing out the rest of the content.)

Again, this might or might not be an issue with webcomic, if you think transcripts.php does everything perfectly please point me to some books/examples where I can learn the proper way of handling this.

Regards, Gary

mgsisk commented 10 years ago

Apologies for the late reply, Gary, but – assuming you haven't already tried this – try modifying the /inkblot/webcomic/transcripts.php so that the global $post variable is restored without using wp_reset_postdata(). At the top of the template, use:

global $post; $old_post = $post;

And then in place of wp_reset_postdata(), try:

$post = $old_post;

My guess is that the problem is technically with wp_reset_postdata(), which resets $post to the main loop post data (which, on a WordPress page, would be the page post data). Assigning the actual current $post to a temporary variable and then reassigning it to the global $post variable is the only way I know to get around this limitation (and I may update Inkblot to use this method instead of wp_reset_postdata() in the future).

GaryTomato commented 10 years ago

Hi Mike,

Your workaround worked perfectly and it's better than what I used before so I wanted to note it for others. (I didn't want to rewrite transcripts.php but this small modification makes it run much better than reinitializing the queue.)

I think you are right and the issue is with wp_reset_postdata(). If I have a chance, I'll raise it with the WordPress developers but right now I'm occupied with making a new theme WebComic-compatible.

Thank you for your help.

Regards, Gary