Add a custom field in a woocommerce category - php

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

Related

Display ACF custom fields on WooCommerce store home and category listing

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/

Properly displaying thumbnails with the_post_thumbnail()

I have created a custom post type named bateau in functions.php. I also created a page where all the posts that belong to the bateau custom post type are displayed, showing some of the most important fields. Finally, when I click on one of those posts, a link sends me to the related custom post page, i.e. a particular type of boat.
The custom post bateau has its own custom fields, a thumbnail, and also its custom taxonomies.
I want to retrieve and display, when I'm on the page of a particular boat, not only the most important but all its custom fields, its thumbnail and its taxonomy.
So, in the functions.php, I have written this filter :
add_filter('the_content','add_text');
function add_text($text) {
global $post;
$text = "";
if($post->post_type == 'bateau') {
$text.= "<h1 class=\"bateau-entry-title\">".get_post_meta( $post->ID, 'bateau_nom', true )."</h1>";
return $text;
}
}
It works fine, provided that I don't write plain HTML text within closing and opening PHP tags, i.e. it only works if I wrap all the HTML in a PHP text var. If I don't do so, the content is also displayed at the beginning of the header, not once, but twice. Strange, isn't it ?
If I add this line :
$text.= "<img class=\"thumb\" src=\"the_post_thumbnail();
the thumbnail displays properly in the "article"...but, guess what, also at the beginning of the header, not once, but twice !!! I just can't find why the thumbnail behaves like this. Anyone can help please ?
In Wordpress, the page you called:
when I'm on the page of a particular boat
is called single. instead of putting a filter on the_content using
add_filter('the_content','add_text');
you would better add whatever you want to the loop. To do so, you may create a file called single-bateau.php (this is a protocol, so you need to name it exactly as this) in your current theme's root directory and there you may have your loop:
if(have_posts()){
while(have_posts()){
the_post();
the_title();
if( has_post_thumbnail() ){
the_post_thumbnail( $thumb_size );
}
the_taxonomies();
the_category();
the_tags();
}
}
I don't think you need the img tag if you're using the_post_thumbnail().
Try doing something like:
if ( has_post_thumbnail() )
the_post_thumbnail();

Display image after taxonomy text with advanced custom fields

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.

How do you get woothemes shortcodes to work on an archive page of product attributes

I am not really sure how to ask this question, however if you have a look at this link you will see what I mean
http://www.skirentalwhistler.com/skill-level/beginner
You will see that the shortcodes for a [box] are not being executed.
If you browse a single product, the shortcode works fine - eg click on the first product called 'Compact' and you will see the [box] is executed into the grey box with one grey and one orange button in it.
Also, I have spent a few hours now trying to work out which template is creating this page. The page is a listing of all products with a certain product attribute value (attribute is skill-level and value is beginner)
I expected it to be archive-product.php but it doesn't seem to be, and I can't find any other file dealing with multiple products on a single page
Any help is appreciated!
When you are using a shortcode in a post, then the [box] syntax is what to use. However, if you are doing a shortcode in a template, you must execute the shortcode with php. Since your homepage appears to just display the short code instead of running it, then it is probably in a template. To execute a shortcode in a template, do this
<?php echo do_shortcode('[box]'); ?>
so for the one on your home page
<?php echo do_shortcode('[box size="small" style="rounded" border="full"]'); ?>
Ok, I have worked out how to do it.
Big thanks to chiliNUT for their help, as they helped me to keep going and going.... thus I gave them an uptick
Anyway, it was using the themes archive.php template (not the woocommerce plugin templates as I presumed). In archive.php, I changed the following line in the div class 'entry'
<?php if ( $woo_options[ 'woo_post_content' ] == "content" ) the_content(__( 'Read More...', 'woothemes' )); else the_excerpt(); ?>
to
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
It now works and doesn't seem to have broken anything else.

Any idea why Wordpress's inCategorgy tag is not working?

So I am trying to add a "-none" to a class for a post if it is in a specific category in Wordpress. So like lets say if I am viewing a post that has a category id of 7, i want a certain class titled "example" change to "example-none".
Here's my code:
<div class="example<?= is_category('events') ?'-none':'' ?><?= in_category('7') ?'-none':'' ?>">
The weird thing with the code is, it works in a page when I am viewing all the posts in a specific category. But when I go to an interior post that is in a specific category, the code does not work.
I am using the in_category('7') tag to achieve this on a wordpress sidebar.
Any idea on what I am doing wrong?
I would remove the quotes around the id of the category:
in_category(7)
This should be a number, not a string.
Thanks. i got it working using this code:
<div class="example<? wp_reset_query(); ?><?= in_category(7) ?'-none':'' ?>">

Categories