How to use custom loop for Wordpress custom post-type and its archive
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I'm using a child theme I started based off of twenty-twelve. I made a custom template called archive-work.php (matching my post type name) and was going change the loop to wrap every 2 posts in a div.

I noticed there's a comment in twentytwelve's loop (in archive.php) mentioning using content-[post-type-name].php to format the loop, but it seems like content-work.php doesn't actually get used out of the box. I don't know how to fiddle with the loop to make it use a wrapper div per two articles, and to close it if it happens to be an odd number like 5 (the last wrapper would contain the 5th article/post only). I'd like to have a custom post-types archive template where two articles are wrapped by a div to simulate 2 columns (which are actually rows of two posts).

If I could get the archive to follow a new template, I assume I could also tell it to only output a custom-sized featured image that would show after the title instead of the entire post content.

I'm a designer with a bit of coding chops, but I'm not an all-out dev–so any laymen's terms would be top-notch. If it helps, check the live site.

I'm trying to achieve this:

<div class="work-row">
  <article>
    <h1>title</h1>
    <a><img src="featured_image.jpg" /></a>
</article>
  <article>
    <h1>title</h1>
    <a><img src="featured_image.jpg" /></a>
  </article>
</div>
<div class="work-row">
  <article>repeat above</article>
  <article>repeat above</article>
</div>
<div class="work-row">
  <article>repeat above</article>
</div>
Explain briefly what you want
eji001 6 years ago
Code for a div to wrap 2 looped posts in an custom post-type archive that works with twentytwelve. See similar and similar I'm lost on how/where the loop gets used and where to put code. I made a content-work.php with this code but didn't change.
ekloff 6 years ago
Also trying to get archive template to show only featured image instead of entire post (no meta or text beside the heading).
ekloff 6 years ago
awarded to weslly

Crowdsource coding tasks.

1 Solution

Winning solution

The content- prefix should be used for post formats, which are different from post types. If you need to create an archive for your post type, you will need to create a archive-work.php.

Here's the solution's code:
https://gist.github.com/4356984

Due to bountify's character limit, I might not have made clear that I am using archive-work.php which plugs in ok. But the loop inside that file seems to reference the content
ekloff 6 years ago
Did you try the file in the link?
weslly 6 years ago
Okay, I got a chance to plug it in and it seems to recognize the change on the archive directly, so that's good. However, it's not getting recognized when set as the homepage with this code in functions.php. It looks like it uses a different loop in that case if you look at it from my homepage.
ekloff 6 years ago
If you want to show the archive at the front page, you should replace your theme's index.php with the code in archive-work.php.
weslly 6 years ago
Almost there....That seems to make sense, but now index.php has no idea about my custom post-type. The front page now shows blog posts, which breaks showing "work" posts. I found a matching question but the code I'm using seems to work in a different way than the proposed solution. Is there a way to tweak index.php now that it has the code of archive-work.php or maybe my function could be reworked to recognize the post-type?
ekloff 6 years ago
This should work: https://gist.github.com/4363051
weslly 6 years ago
It's works like a charm. Appreciate that.
ekloff 6 years ago
A quick followup bug using this code is that the homepage does not paginate properly, showing the static set of images when clicking "older posts." mydomain.com/page/2 shows doesn't update. Any suggestions?
ekloff 6 years ago
How many posts do you have under the custom post type?
weslly 6 years ago
Right now its something like 14 published posts, but it will be ever increasing in the future.
ekloff 6 years ago