how to insert yoast main category in advanced custom fields?

I want to use yoast's main category when building blocks in advanced custom fields

        //Primary Yoast Category
        $primaryCat = new WPSEO_Primary_Term('category', get_the_ID());
        $primaryCat = $primaryCat->get_primary_term();


        $catPrimaryName = get_cat_name($primaryCat);
        $catPrimaryLink = get_category_link($primaryCat);
        $context['catPrimaryLink'] = $catPrimaryLink;
        $context['catPrimaryName'] = $catPrimaryName;

// ----------

1 answer

  • answered 2019-08-06 15:07 FernandoGiroto

            if (is_array($modules['categories']))
            add_filter('block_categories', function ($categories, $post) use ($modules) {
                $module_categories = [];
                foreach ($modules['categories'] as $module_category)
                    $module_categories[] = [
                        'slug' => $this->block_category_slug . '_' . $module_category,
                        'title' => $this->block_category_title . ' - ' . $module_category
                    ];
    
                return array_merge($module_categories, $categories);
            }, 10, 2);
    
        // add our blocks & fields
        foreach ($modules['modules'] as $block) {
            acf_register_block(array(
                'name' => $block['name'],
                'title' => __($block['title']),
                'description' => __($block['description']),
                'render_callback' => [$this, 'my_acf_block_render_callback'],
                'category' => $this->block_category_slug . '_' . $block['category'],
                'icon' => 'layout',
                'align' => 'wide',
                'keywords' => array_merge($this->default_keywords, isset($block['keywords']) && is_array($block['keywords']) ? $block['keywords'] : []),
            ));