Check for ACF and a specific ACF field and display if possible. #122

Merged
merged 3 commits into from Jul 22, 2014

Projects

None yet

2 participants

@tlongren
Contributor

I've been using this setup to allow me to choose which category is shown when viewing a single post. ACF could be setup in various ways, but as long as the field is named primary_category, and it's not a file field, this should work.

If ACF and the primary_category ACF field don't exist (ie: the plugin isn't enabled or hasn't been configured), we fall back to the original category display.

Thought others may like this. I'll have a blog post about my exact setup later on today.

@raamdev
Owner
raamdev commented Jul 21, 2014

@tlongren Thanks so much for the Pull Request! I love this idea and would be glad to accept the PR if you can modify it to work without the Advanced Custom Fields plugin (i.e., if someone creates the primary_category Custom Field manually, it should work just the same).

You should be able to simply change this:

if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) && get_field('primary_category') )

to this:

if ( get_field('primary_category') )

Also, to prevent any possible naming collisions, I would like to call the field independent_publisher_primary_category. I know it's a lot longer, but you'd only need to type it once anyway.

@tlongren
Contributor

Excellent points @raamdev, and I totally agree. I had forgotten get_field() would also grab the standard WP custom fields, too.

So, I'll get this fixed up this afternoon and attach the commits here. Thank you, sir!

@tlongren
Contributor

Here you go, @raamdev.

Updated custom field name and only checking for that field
tlongren@7e5f648

Missing word in a comment, :/
tlongren@27319cf

Seems to work well, it's running at longren.io right now, but only for the most recent 10 or so posts maybe.

@raamdev raamdev added this to the Next Release milestone Jul 22, 2014
@raamdev raamdev added the enhancement label Jul 22, 2014
@raamdev raamdev merged commit 2822ada into raamdev:master Jul 22, 2014
@raamdev
Owner
raamdev commented Jul 22, 2014

Merged. Thank you! :)

@raamdev
Owner
raamdev commented Aug 12, 2014

@tlongren I noticed while testing the dev branch of Independent Publisher that I was getting PHP notices about get_field() not existing. I see now that get_field() is not a WordPress function but rather an ACF plugin function. I'm updating the code to replace this:

get_field('independent_publisher_primary_category')

with get_post_meta():

get_post_meta( get_the_ID(), 'independent_publisher_primary_category', true )

I assume that since the ACF plugin just works with WordPress custom fields, this code change shouldn't affect the goal of your Pull Request here, but if that's not true, please do let me know. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment