I
I
Ivseti2021-03-18 18:46:13
WooCommerce
Ivseti, 2021-03-18 18:46:13

How to display different shortcodes in different Woocommerce categories using Advanced Custom Fields?

I have a task in each of the created categories to display different shortcodes in a certain place.

Currently installed Advanced Custom Fields .

Configured like this: 312737e235.jpg

As they say on the Internet, I tried it by pasting the code:

$term = get_queried_object();
$my_gallery = get_field('my_gallery', $term);
echo $my_gallery;


In the archive-product.php file. But nothing worked. This is of course half the battle, the most interesting thing is how to insert a shortcode, for example, above the list of products or below the list of products.

Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Litvinenko, 2021-03-18
@Ivseti

what you are doing in the screenshot is you are attaching a custom field to posts that have that category. To display it in the product archive in each card that has such a field, it is enough to use the simplest function the_field('slug'), well, you can also tighten the check. only this is a card template, tobish content-product.php
but as far as I understand, you need to display the field outside the cycle on the archive page, then your code is correct and the template is also correct, and you need to bind the field to Taxonomy equals Category. Fields will appear in product categories. Insert the actual sham shortcode into the text fields and display it like this

$term = get_queried_object();
$my_gallery = get_field('my_gallery', $term);
echo do_shortcode($my_gallery);

Step by step so that it’s already quite simple
1. Create a field
6053b2a7ca2a8003335811.png
2. Insert a shortcode in the desired category
6053b2d7a1794757206928.png
3. I decided not to edit the template, but to hang the output on the hook, on which the category description and taxonomy description are already hanging
function archive_product_shortcode() {
  $term = get_queried_object();
  if ( function_exists('get_field') ) {
    $my_shortcode = get_field('my_shortcode', $term);
    echo do_shortcode( $my_shortcode );
  }
}
add_action('woocommerce_archive_description', 'archive_product_shortcode', 20 );

Voila, a shortcode appeared above the products
6053b36444934913124909.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question