Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Andrew Folts
Andrew Folts
14,238 Points

What's the best way to add custom fields to Wordpress archives?

It seems there's two ways to do this, and BOTH kind of suck...

  1. Create a page template and add a custom field on each page with a key to tell the template which post type to get.

  2. Use archive.php and get fields for specific archive pages by storing the info in an option page somewhere.

These aren't great solutions, because they both rely on setting a key or getting a field based on the archive name, so if you change one of those, your scheme is screwed.

How do other people handle this? Any better ideas?

Thanks!

1 Answer

Hi Andrew,

This is very situational. Can you give more detail on the end goal?

Generally, when I have to do something similar I will use the ID of a given author, category etc to change the template this allows more flexibility as well if you change somethings up.

Example - Slug changes or Author name changes, using the ID avoids this breaking it.

Andrew Folts
Andrew Folts
14,238 Points

This always happens, haha. Literally right when your comment came in, I was messing around with ACF and realized you can actually add custom fields to taxonomy term pages.

It's a bit annoying, because it seems you have to set a field location rule to all terms individually, but maybe there's a workaround.