Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from Jul 22, 2014

Conversation

tlongren
Copy link
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
Copy link
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
Copy link
Contributor Author

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
Copy link
Contributor Author

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 added a commit that referenced this pull request Jul 22, 2014
Check post custom field for primary category on single posts

This allows a site owner to show only one category on the post when the post has been assigned to multiple categories.
@raamdev raamdev merged commit 2822ada into raamdev:master Jul 22, 2014
@raamdev
Copy link
Owner

raamdev commented Jul 22, 2014

Merged. Thank you! :)

@raamdev
Copy link
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants