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.
Check for ACF and a specific ACF field and display if possible.
@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') )
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.
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!
Support native WordPress custom fields, too.
Fix crap grammar.
Here you go, @raamdev.
Updated custom field name and only checking for that field
Missing word in a comment, :/
Seems to work well, it's running at longren.io right now, but only for the most recent 10 or so posts maybe.
Merged. Thank you! :)
@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_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. :)
Replace plugin-specifc get_field() with WP get_post_meta(). See #122