I have created a custom post type called:'activities' and created a taxonomy for it called: 'activity_locations'. I have then added a custom field to the taxonomy for an image using Advanced Custom Fields, this image field is called: 'activity_location_image'
On the single template I have managed to display the taxonomy for the product with the following code:
Available in: <?php the_terms( $post->ID, 'activity_locations', ' ', ' / ' ); ?>
However I need to elaborate on this to add a small image after the taxonomy text. I have tried the following code but it hasn’t worked (nothing is displaying):
<?php
$term = get_field('test');
if( $term ): ?>
<img src="<?php echo get_field('activity_location_image', $term); ?>" />
<?php endif; ?>
Can anyone offer any advice/assistance on how to make this work?
If you are still on the single template page, then get_field() without a second argument will only be fetching metadata for your current post object, not the related term objects.
If you read the documentation for get_field() you'll see that to query for term fields you need to use the format
get_field('field_name', 'taxonomyname_X)
Where taxonomyname would be activity_locations and X would be the term ID.
Since you want to customize the display and use the data about the terms, you'll have to swap the_terms() with wp_get_post_terms() which will give you an array of term objects. You can then loop over the array, grabbing the image for each term and outputting the HTML to display the term.
Related
I am trying to display Advanced Custom Field on my WooCommerce storefront in html list tag. I modified the file:
content-product_cat.php
from this:
<li <?php wc_product_cat_class( '', $category ); ?>>
to this:
<li style="background: <?php the_field('my_ACF_field'); ?>" <?php wc_product_cat_class( '', $category ); ?>>
And also I added ACF field on my WP backend and assigned it to my page called "Store" which is the WooCommerce storefront. I can fill the input on my backend (Store page) but it's not showing on storefront/category page, it's just not passing. Where is the problem?
I also tried to set this field as taxonomy, and display it on category setting in WooCommerce, also without success.
I think you'll need to use the second parameter on ACF the_field, to get the value for the specific object that you're after. E.g. if you have the ACF Field set on a product category, you'll need to pass in the term that you're currently looking at to get the right value.
I believe that on a category page you'll need to use something like
<li style="background: <?php the_field('my_ACF_field', $category);?>" <?php wc_product_cat_class( '', $category ); ?>>
This ACF documentation field gives more detail about adding fields to taxonomy terms: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
I have added a custom taxonomy to Users - user categories.
Using the code below, I am able to output the custom taxonomies in each edit-user profile page:
function show_user_category( $user ) {
//get the terms that the user is assigned to
$assigned_terms = wp_get_object_terms( $user->ID, 'user_category' );
$assigned_term_ids = array();
foreach( $assigned_terms as $term ) {
$assigned_term_ids[] = $term->term_id;
}
//get all the terms we have
$user_cats = get_terms( 'user_category', array('hide_empty'=>false) );
echo "<h3>User Category</h3>";
//list the terms as checkbox, make sure the assigned terms are checked
foreach( $user_cats as $cat ) { ?>
<input type="checkbox" id="user-category-<?php echo $cat->term_id ?>" <?php if(in_array( $cat->term_id, $assigned_term_ids )) echo 'checked=checked';?> name="user_category[]" value="<?php echo $cat->term_id;?>"/>
<?php
echo '<label for="user-category-'.$cat->term_id.'">'.$cat->name.'</label>';
echo '<br />';
}
}
The code above simply displays a list of checkboxes, ordered by term_id.
Of course, I want to display them in the same way that custom taxonomy terms would be displayed in a custom post type (a scrollable list of checkboxes, with child terms indented and underneath their parent term). The code above does not display the terms in order of parents/children.
Is there a WP function I can pass my taxonomy & terms to, to create what I described in the paragraph above? Or do I have to do it manually?
Thanks
Short answer:
Yes, there are. They are called post_categories_meta_box() and post_tags_meta_box(), depending on the type of taxonomy you are using.
Long answer:
This is the sort of question that can be answered by inspecting the WP core code.
Here's the walk-through on how I did it to answer this question:
Editing a page in admin results in a url of wp-admin/post.php. So, inspect that file (post.php).
A quick search in that file for "tax" results in nothing, so we can at least look at the case 'edit' section, because we know we're doing an edit. And we can see it includes a file edit-form-advanced.php.
So, inspect that file (edit-form-advanced.php)
A quick search for "tax" gets into an area that looks promising.
Here we see they call get_taxonomy, and then that get_taxonomy object that is returned has a callback called meta_box_cb - so do a cross-file search for that. We find several instances, but the instance in taxonomy.php is the most promising (because of the file name), so lets look there.
It appears (around line 430 of taxonomy.php) that there's a pair of callback functions - post_categories_meta_box and post_tags_meta_box that WP core is using. Let's check them out - do a cross-file search for function post_categories_meta_box, and we find both of these functions exist in the file meta-boxes.php. The comments for the functions are excellent, and tell us what you are asking: "Display post categories form fields".
Armed with this information, we can ask Google, and we find the WP documentation for the functions:
post_categories_meta_box
and
post_tags_meta_box
Hi I have created with CTP custom fields plugin a custom field to appear only in taxonomy -product category- this field, appear in the back end when I edit the category.
Field is intended to be there to upload images and output the URL of the image like this:
<div class="page-heading" style="background-image:url('<?php the_field('field_name'); ?>') !important;">
This kind of code works perfectly OUTSIDE woocommerce (in the normal WP pages and posts) but for some reason, doesn't show anything, even that in the back end I can see the image attached to the category.
In the front end it shows like a empty field...
Im not sure what i'm doing wrong.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
the_field( 'field_name', $post->ID );
Thanks, your tips made me search the right thing and found the answer:
<?php
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
the_field('the_name_of_the_field', $post_id);
?>
I'm having trouble showing ACF on a custom taxonomy page.
The custom tax is 'destinations' with the page being taxomony-destinations.php the field is called 'destination_landing_image. I'm trying to display it on 'mysite.com/destinations/coutryname' as follows:
<?php if (have_posts()) : while (have_posts()) : the_post();
$destination_landing_image = get_field('destination_landing_image');
<img src="<?php echo $destination_landing_image['url'] ?>" alt="<?php echo $destination_landing_image['alt'] ?>">
<?php endwhile; endif; ?>
Oddly enough the page in question does show up the ACF fields within the custom post type(holidays) which is lower in the page. So I guess firstly am I calling it correctly within the loop? and Secondly is a custom taxonomy the right type of page to use?
When you use ACF with posts you can just use get_field() as you are, but when you use it with anything else such as taxonomies, users or options, you need to give ACF a hint about how to look it up. In this case you need to tell it what taxonomy and term you are specifically targeting. Luckily both WordPress and ACF make this really easy:
//Ask WordPress for the currently queried object
//http://codex.wordpress.org/Function_Reference/get_queried_object
$obj = get_queried_object();
//Pass that object as a second parameter into get_field
$destination_landing_image = get_field( 'destination_landing_image', $obj );
I am trying to insert PHP code in a shortcode.
For now it look likes this:
<?php echo do_shortcode('[to_like][/to_like]'); ?>
<?php do_action('single_spot_after_content', get_the_ID(), 'after_content'); ?>
I have tried several plugins to combine this to one shortcode (PHP shortcode plugin, Advanced Custom fields, etc.)
This is what I want to accomplish, but it is not working:
<?php echo do_shortcode('[to_like]<?php do_action('single_spot_after_content', get_the_ID(), 'after_content'); ?>
[/to_like]'); ?>
In the front-end the user can select the value through a dropdown field --> for example 10% discount (this is going to be displayed when a visitor likes the box). The theme has a special markup field (you can create custom submission fields) where %% is going to be changed by the chosen value from the customer. This value is going to be displayed on the website (singe listing page).
How can I implement this? This is so vital for the website.
Try this:
<?php echo do_shortcode( '[to_like]' . do_action('single_spot_after_content', get_the_ID(), 'after_content') . '[/to_like]' ); ?>