Home > front end >  Get selected category list array from category tax custom fields for sidebar menu
Get selected category list array from category tax custom fields for sidebar menu

Time:01-14

I am developing a custom theme. I needed a custom menu on the category listing page (archive.php). I need to list selected categories in this menu. I added taxonomy field with ACF. This is multi-selectable. But I can't list them in archive.php. (No subcategories will be used)

The schema is as follows:

Category : "News" Categories to be displayed in the sidebar content when viewing the news page: "Sports", "Politics" etc.

Category: "Announcements" Categories to be displayed in the sidebar content when viewing the Announcements page: "Politics", "What's New", "Innovation" etc.

While editing each category, the editor will be able to select the categories to be listed in that category's sidebar. I did this with ACF. But I can't list it in archive.php.

Example query:

<?php $categories = get_field('categories_in_category', $taxonomy->term_id); var_dump($categories); ?>

This code returns null.

I tried many methods but they all return null.

UPDATE (SOLUTION)

For this to work, you need to prefix the parameter with "category_". example usage:

<?php
$parameter_for_taxonomy_query = 'category_'.$taxonomy->term_id;
get_field('field_name', $parameter_for_taxonomy_query);
// $taxonomy is main query object from archive.php or others...
?>

Thanks to @tyzia

CodePudding user response:

Try this way:

$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$value = get_field( 'categories_in_category', $term_id );

Here you get term id of a taxonomy and get your ACF field for this term_id.

More on the topic: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

  • Related