Yala Yala Herzlin
Yala Yala Herzlin

Reputation: 632

Add a custom field in a woocommerce category

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.

Upvotes: 0

Views: 751

Answers (2)

Yala Yala Herzlin
Yala Yala Herzlin

Reputation: 632

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);
?>

Upvotes: 0

Noman
Noman

Reputation: 4116

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 );

Upvotes: 0

Related Questions