programing

Dynamically populate an ACF field (radio) on a multisite install

telecom 2023. 10. 17. 20:02
반응형

Dynamically populate an ACF field (radio) on a multisite install

I'm trying to populate an Advanced Custom Fields' radio field pulling out the data from a custom post type located in the main blog of a multisite install.

For a better understanding I made this simple flow graphic.

Flow

So I created a function in order to pull out the data from Main Blog and show as radio items on child site.

The function looks like this and I used this as reference

function getctas($field) {
    $field['choices'] = array();

    switch_to_blog(1);

    $args = array(
            'post_type' => 'location_icons',
            'posts_per_page' => '-1',
         );

        $ctas = new WP_Query( $args );

        while ( $ctas->have_posts()) {
            $ctas->the_post();

            $choices = get_field('icon',false);

            $choices = explode("\n", $choices);

                    foreach( $choices as $choice ):

                        $field['choices'][ $choice ] = '<img src="'.$choice.'"/>';

                    endforeach;

        }
        restore_current_blog();
        return $field;

}
add_filter('acf/load_field/name=call_to_action_icon', 'getctas');

옵션이 올바르게 나열된 경우(옵션은 이미지), 필드를 성공적으로 꺼냈습니다.icon메인 블로그에서 라디오 레이블과 가치를 제공합니다.

The issue I'm having is that once the post is saved when I query it on the child's page template I get the correct images but the title of the post on blog 1 repeated. The ideal would be to have:

  • Image
  • Child Blog Post's Title
  • Child Blog Post's Desc

And what I'm, instead getting is:

  • Correct Image
  • CTP Title that contains the image on blog 1
  • No description

  • Correct Image

  • Same title as previous one
  • No description

And so on.

If any of you need more clarifications to help me solve this I'd be pleased to explain further.

신고해주세요global $switched;메인블로그로 전환하기 전에 wp가 글로벌 변수를 선언한 후 작동하지 않으면 현재 블로그로 다시 전환하지 않는 것 같습니다. 이것을 시도해 보세요.

기본 블로그로 전환하기 전에 현재 블로그 ID 가져오기$current_site =get_current_blog_id();

once you are done. switch it back using

switch_to_blog( $current_site );
$GLOBALS['_wp_switched_stack'] = array();
$GLOBALS['switched']           = FALSE; 

ReferenceURL : https://stackoverflow.com/questions/24023680/dynamically-populate-an-acf-field-radio-on-a-multisite-install

반응형