Skip to content
Advertisement

Adding different text fields in each category footer using enhanced custom categories plugin in wordpress

I have been trying for hours to display an advanced custom field in the footer area in order to change the category text according to each category. As soon as I leave the scope of the content-ecp.php file, the category ID changes.

When using the $product_cat_id in the_field('content_under_products', $product_cat_id);?> to retrieve the field data..

I don’t get the correct ID where the field content is stored. The correct ID would be (1034) but I am getting (108). I know, I could change the code to the_field('content_under_products', 1034);?> but this would only work for one specific page which wouldn’t make much sense side-wide.

There must be a way to select the other ID. Unfortunately, my knowledge of php is very little. So I didn’t manage to come up with a proper solution with a dynamic post ID.

I would be extremely glad if anyone could help me out with this.

Here is a link to the page : https://praesenteente.de/geldgeschenke/ I basically consists of:

CATEGORY HEADER AND DESCRIPTION (here, the field can be displayed correctly)

WOOCOMMERCE PRODUCTS

FOOTER (here, it doesn’t show up)

Advertisement

Answer

It sounds like you only have access to your $product_cat_id inside the content-ecp.php file.

I think there is a logic issue you should check: The content-pages are for single views, so when there is an id for a single post/page and you want to have them look the same for all single posts.

An archive page is viewing a collection of posts. The name of your variable PRODUCT_cat_id using not the plural of products, lets me assume you are setting the value of the variable like this:

$product_cat_id = get_the_ID();

So the id of the current viewed post is saved to the variable. This is working fine in a single page, but not in archives.

When you are viewing a category, you have an id for the term you are viewing. For example the category is called “category” and you are now viewing “shoes”, which is a category term. You can get the id of the term of the current viewing category term page by using:

$cat_term_id = get_queried_object()->term_id;

Now you have saved the id of the category term. With this id you can now get your category description text, which is a meta field in your category term.

$cat_description = get_field('content_under_products', $cat_term_id );

This is working, if your ‘content_under_products’ field is an field added with acf to categories. I think this is what you are looking for.

If I got your question wrong and you just want to show the description for the archive itself (and not for the terms of the category / taxonomy), you can just use:

$archive_description = get_the_archive_description();

https://developer.wordpress.org/reference/functions/the_archive_description/

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement