• Resolved hashimnaushahi

    (@hashimnaushahi)


    Hi there,

    Newbie here!

    I’m a beginning WordPress developer and I’m working on my first client website for free so that I’m getting some practice.

    Now I’m running against two problems which I don’t know how to solve. I already created a topic for the first one. And for this topic I hope you can help me with the second.

    Your help will not only result in solving the problem but also in my learning. So I thank you in advance!

    To understand the issue…

    I’ve created a custom taxonomy with CPT UI. And then I created a custom field of the field type ‘image’ for that custom taxonomy with ACF.

    Normally, when a post has a custom field, returning or displaying its’ value is relatively straightforward with the get_field() or the_field() and in the case of an image also the wp_get_attachment_image() function. However in this case, I’m trying to show the image of a custom field that’s inside a custom taxonomy.

    To be more precise, this concerns a webshop that sells food supplements. And I created a custom taxonomy called ‘certificates’ for WooCommerce products. Those certificates are for things like ‘organic’, ‘non-gmo’, ‘vegan’, etc.

    With this setup I can select for every product which certificates apply. And it’s possible for a product to have multiple certificates.

    Now, I’d like to show the images of all the certificates that apply for a specific product on the single product page of that product. And I’ve got no idea how to accomplish that with the functions made available by ACF because the custom field for those images isn’t inside the product itself but inside the custom taxonomy that’s selected for that product.

    I hope somebody can help me solve this. Or at least help me get just a tiny step forward.

    I’m looking forward to your support. Thank you in advance!

    Sincerely,

    Hashim

Viewing 9 replies - 1 through 9 (of 9 total)
  • hi @hashimnaushahi

    you can get a field value from a taxonomy using get_field() by specifying the taxonomy and term ID in the second parameter – joined by an underscore.

    for example, to get the value of the ‘certificate_image’ field from the ‘certificate’ taxonomy for the term with an ID of 7: get_field( 'certificate_image', 'certificate_7' );

    in the case of your specific example, you’ll need to first get a list of the certificate terms associated with the post and then retrieve the image for each:

    	global $post;
    	$terms = get_the_terms( $post, 'certificates' );
    	foreach ( $terms as $term ) {
    		$thumbnail = get_field( 'certificate_image', $term->taxonomy . '_' . $term->term_id );
    	}

    hope that helps!

    • This reply was modified 2 years, 7 months ago by Lucas Karpiuk. Reason: formatting
    Thread Starter hashimnaushahi

    (@hashimnaushahi)

    Hi @karpstrucking,

    Thank you so much for your reply!

    You’ve helped me so much. I’ll give this a try right away and keep you updated.

    🙂

    Thread Starter hashimnaushahi

    (@hashimnaushahi)

    Hi @karpstrucking,

    Unfortunately, I can’t get the code to work. And I hope you’re willing to help me out just a bit further.

    And by the questions I’m asking you’ll definitely be able to tell how much of a rookie I actually am. So, my apologies in advance. 🙁

    I mixed your code with the code provided by ACF to display the image and then turned it into a shortcode. And this is the result:

    
    
    <pre><code>function na_display_certificates() {
    	
    	global $post;
    	
    	$terms = get_the_terms( $post, 'certificates' );
    	
    	foreach ( $terms as $term ) {
    		
    		$image = get_field( 'certificate', $term->taxonomy . '_' . $term->term_id );
    		$size = 'medium'; // (thumbnail, medium, large, full or custom size)
    		
    		if( $image ) {
        		
    			echo wp_get_attachment_image( $image, $size );
    		
    		}
    		
    	}
    		
    }
    
    add_shortcode( 'certificates', 'na_display_certificates' );

    Also, the theme I’m using is Kadence. And they offer Kadence Elements. So I created an element with the shortcode [certificates] and with the help of hooks set it to display in the short description of the single product page.

    However, the images aren’t showing.

    I know the element works because I also filled that element with just text as a test. And that text is showing up.

    So I know my code isn’t right.

    Do you think you can help out this rookie developer with this?

    Thank you in advance!

    Sincerely,

    Hashim

    • This reply was modified 2 years, 7 months ago by hashimnaushahi. Reason: typo
    • This reply was modified 2 years, 7 months ago by hashimnaushahi.

    @hashimnaushahi when you edit your custom taxonomy in CPTUI, what is the value of “Taxonomy Slug”?

    Thread Starter hashimnaushahi

    (@hashimnaushahi)

    @karpstrucking

    Thank you so much for sticking with me!

    The taxonomy slug of that custom taxonomy in CPT UI is ‘certificates’.

    Additionally, the field of name of the custom field of the field type ‘image’ in ACF which I added to that custom taxonomy is ‘certificate’.

    Does that help somewhat?

    Thread Starter hashimnaushahi

    (@hashimnaushahi)

    Hi @karpstrucking,

    Was the additional information useful in any way or are you just as stuck as I am? 🙁

    @hashimnaushahi okay let’s try some debugging. add print_r( $post ); after the global $post; and check if it’s outputting a WP_Post Object.

    Thread Starter hashimnaushahi

    (@hashimnaushahi)

    @karpstrucking, your code works!

    I was getting an error because I forgot to add certificates in the product I’m testing. After I added certificates it worked perfectly!

    Sorry I bothered you with this. And thank you so much for your help! 🙂

    • This reply was modified 2 years, 7 months ago by hashimnaushahi. Reason: Marked ticket as resolved

    @hashimnaushahi no bother at all, glad to hear you got it sorted out!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Show value of custom field of custom taxonomy’ is closed to new replies.