There is another dead simple way to do this without an options page (but using an option page for items displayed throughout the site, like footer info, can provide a more logical place for clients to enter things).
Here is an example of a field displaying a newsletter link on the homepage:
<?php the_field( 'newsletter'); ?>
Here is the same code but modified to display in the side bar of all other pages of the site:
<?php the_field( 'newsletter', 2 ); ?>
The “2” is the post ID of the homepage – that’s it!
FYI this is hardcoded into my custom theme files.
I hate to resurrect an old thread, but this is too valuable, and out of the entire internet, is the only simple and iron clad solution to adding an ACF field to Woocommerce that I’ve found. It even adds the data to the emails:
https://stackoverflow.com/a/59355471/15160691
Its working for me!
I may or may not post new code as well – I handed the above working to my co-worker and she says she improved it a ton and did it much more simply, though I’m sure very similar. If I can wrangle that code (scrambled eggs) I’ll post it as well!
But yeah, I’m no pure developer myself either. With so many options to do this I hope a developer might find an easy go of it!
I actually found that site Google Searching “advanced custom fields coupon code” and found the plugin at a discount (wish I just found the coupon code I was after!). I had the Developer Plugin in my cart on advancedcustomfields.com and was ready to pull the trigger and download. Of course its relatively cheap – but with three kids and an employee, you look for whatever deal you can find 🙂 – I didn’t research wpspring.com, I thought they were an affiliate of some sort.
Word of warning – I JUST purchased AFC from wpspring.com and it looks like I paid a discount (which is why I got it there) but it is only for a year AND I’ll have to do manual updates on all my client sites 🙁 So it was cheaper now, but I’ll have to come back here and buy a proper PRO Developer license (which I had in my cart until I decided to do a search for a coupon code). Wpspring.com makes you renew each year which of course is more expensive. All I can say is uuuughh! Buyer beware on other sites!
Great!
The below Solved my problem! Just one note, I had to change the last curly bracket to a bracket just above the Wp_Query. Not having a matching meta_key tripped me up for just a minute too, but temporarily eliminating the reordering proved the code is good! Thanks! This helps some of us continue to make a living! 🙂
The winning code:
<?php
$part_category = get_sub_field('part_category'); // This HAS to be a string value. Make sure that it's not an array or term object.
$args = array(
'post_type' => 'sweeps',
'posts_per_page' => -1, //fetches ALL posts without limit
'order' => 'ASC',
'tax_query' => array(
array(
'taxonomy' => 'category', // If you're using a custom taxonomy this needs to be changed
'terms' => array($part_category[0]),
'field' => 'term_id'
)
)
);
$catquery = new WP_Query($args);
?>
Sorry – my screen shot above was premature. It has some settings that were experiments and I forgot to change them back – you’ll notice I had the category returning a Term Object above – but as you’ll see in this new screen shot, I’m returning the Term ID and still get the same result of not being able to enter the category into the query through a custom field.
Thanks!
Scott
Good snippet! But unfortunately it’s the same situation in that if I manually enter the id of the category, replacing “array($part_category)”, it works, but as it is I don’t get any result from the code.
I’m not sure, but I’m wondering if I’ve set up the custom field wrong, and after that, if there is something funky set up in CPT UI? For now I’ve attached a screen shot from ACF for the field.
Thanks!
I think we’re closer, but no cigar 🙁 but I’ve also moved on to wp-query. I have it set up, but it’s still not pulling the category for the query. Here is my complete loop:
<?php if( have_rows('parts_table') ): ?>
<?php while( have_rows('parts_table') ): the_row(); ?>
<?php
$catquery = new WP_Query( 'order=ASC&orderby=meta_value&meta_key=weight_(lbs.)&showposts=200&cat='.get_sub_field('part_category').'&post_type=sweeps' ); ?>
<table>
<tr><td>Part Number</td><td>Description</td><td>Weight (lbs.)</td>
<?php while($catquery->have_posts()) : $catquery->the_post();
?>
<tr>
<td>
<?php the_field('part_number'); ?>
</td>
<td>
<?php the_field('description'); ?>
</td>
<td>
<?php the_field('weight_(lbs.)'); ?>
</td>
</tr>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
</table>
<?php endwhile;?>
<?php endif; ?>
So there is my loop. If I manually enter a cat id it works, but it won’t pull my ACF field. Thanks so far!
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.