试图检索ACF中的中继器字段的唯一ID,以便在hide/揭示中

发布于 2025-02-12 19:02:22 字数 6766 浏览 2 评论 0原文

我在ACF中有一个中继器字段,并安装了独特的插件。 ( https://github.com/klicher/wp-acf-acf-unique_id

)可以看到生成的唯一ID。在ACF自定义字段区域中,自定义字段的名称为“ unique_repeater_id”。我也有诸如标题,扬声器,字幕,描述之类的字段。他们都起作用,所以我觉得我很亲密。

我的php代码如下,在其中定义$ x并尝试抓住该ID并在按钮中使用它:

<button onclick="myFunction(myDIV<?php echo $x; ?>)">More Details</button>

但是无论我做什么,渲染页面都不会附加唯一的ID,因此我的JavaScript不运行(仅此)渲染MyDiv部分。)

function myFunction(index) {
  if (index.style.display == "block" || index.style.display == "") {
    index.style.display = "none";
  } else {
    index.style.display = "block";
  }
}
<?php
                $args = array('pagename' => 'agenda');
                $agenda_page = new WP_Query($args);

                if($agenda_page->have_posts()) : while($agenda_page->have_posts()) : $agenda_page->the_post();
                    ?>

            <section class="content">


            <h3><?php the_field('agenda_list_title'); ?></h3>
            <?php the_field('agenda_list_intro_paragraph'); ?>

                        <?php the_content(); ?>

                        <?php endwhile; endif; ?>



                 <ul class="event-list">
                    <?php
                            $agenda_posts = new WP_Query(array('post_type' => 'agenda', 'orderby' => 'post_date', 'order' => 'ASC'));
                            while ($agenda_posts->have_posts()) : $agenda_posts->the_post();

                                if( get_field('2021_agenda')) {
                        ?>

                            <li class="event" id="agendastart">
                                <div class="event-time">

                                    <?php
                                    if (have_rows('time_begin')):
                                    while (have_rows('time_begin')) : the_row();

                                    $time = get_sub_field('time');
                                    $time = ltrim($time, '0');
                                    echo $time;
                                    the_sub_field('am/pm');

                                    endwhile;
                                    endif;

                                    echo ' ' . __('&ndash;') . ' ';

                                    if (have_rows('time_end')):
                                    while (have_rows('time_end')) : the_row();

                                    $time = get_sub_field('time');
                                    $time = ltrim($time, '0');
                                    echo $time;
                                    the_sub_field('am/pm');

                                    endwhile;
                                    endif;
                                    ?>
                                </div>

                                <?php edit_post_link('[Edit Event]', '<div class="edit-link">', '</div>'); ?>
                                <div class="indent">

                                    <?php

                                    $c = 0;
                                    if (have_rows('talks')):
                                    while (have_rows('talks')) : the_row();

                                    $title = get_sub_field('title');
                                    $subtitle = get_sub_field('subtitle');
                                    $description = get_sub_field('description');
                                    $x = get_sub_field('unique_repeater_id');
                                    ?>

                                    <div class="pretitle"><?=$title;?></div>

                                    <?php if ($subtitle) : ?>

                                        <div class="event-title">
                                            <?php $content = get_sub_field('description'); if ($content) : ?>

                                                  <?php echo $subtitle; ?>
                                                  <br><button onclick="myFunction(myDIV<?php echo $x; ?>)">More Details</button>
                                        </div>

<div id="myDIV<?php echo $x; ?>" style="display: none"><?php $i++ ;?><?php echo $description; ?> <?php else: ?> <?php echo $subtitle; ?> <?php endif; ?> </div>

                                    <?php endif; ?>

                                    <ul class="speaker-list">


                                        <?php
                                        $speakers = get_sub_field('speakers');

                                        if ($speakers) :
                                            foreach ($speakers as $speaker) :

                                            $label = '';
                                            $mod = get_field('moderator', $speaker->ID);
                                            $intr = get_field('intro_speaker', $speaker->ID);

                                            if ($mod == true) :
                                                $label = ' <span style="color:' . get_field('primary_branding_color', 'options') . '">(moderator)</span>';
                                            endif;

                                            if ($intr == true) :
                                                $label = ' <span style="color: #35a6c4">(introduction)</span>';
                                            endif;

                                            ?>

<li>

<a class='iframe' href="<?php echo get_permalink($speaker->ID); ?>"><?php echo get_the_title($speaker->ID); ?></a><?php echo $label; ?>

    <?php the_field('job_title', $speaker->ID); ?>
</li>

                                            <?php
                                            endforeach;
                                        endif; ?>

                                    </ul>

                                    <?php
                                        $c++;
                                    endwhile;
                                    endif;
                                    ?>


                                </div>
                            </li>

                            <?php } endwhile; ?>

                        </ul>

            </section>

I have a repeater field in ACF with the UniqueID plugin installed. (https://github.com/KLicheR/wp-acf-unique_id)

I can see the unique ID generated. In the ACF custom fields area, the name of the custom field is "unique_repeater_id". I also have fields like title, speakers, subtitle, description. They all work, so I feel like I'm close.

I have php code as follows, where I define $x and try to grab that ID and use it in a button:

<button onclick="myFunction(myDIV<?php echo $x; ?>)">More Details</button>

But no matter what I do the rendered page does not append the unique ID so my javascript doesn't run (it only renders the myDIV part.)

function myFunction(index) {
  if (index.style.display == "block" || index.style.display == "") {
    index.style.display = "none";
  } else {
    index.style.display = "block";
  }
}
<?php
                $args = array('pagename' => 'agenda');
                $agenda_page = new WP_Query($args);

                if($agenda_page->have_posts()) : while($agenda_page->have_posts()) : $agenda_page->the_post();
                    ?>

            <section class="content">


            <h3><?php the_field('agenda_list_title'); ?></h3>
            <?php the_field('agenda_list_intro_paragraph'); ?>

                        <?php the_content(); ?>

                        <?php endwhile; endif; ?>



                 <ul class="event-list">
                    <?php
                            $agenda_posts = new WP_Query(array('post_type' => 'agenda', 'orderby' => 'post_date', 'order' => 'ASC'));
                            while ($agenda_posts->have_posts()) : $agenda_posts->the_post();

                                if( get_field('2021_agenda')) {
                        ?>

                            <li class="event" id="agendastart">
                                <div class="event-time">

                                    <?php
                                    if (have_rows('time_begin')):
                                    while (have_rows('time_begin')) : the_row();

                                    $time = get_sub_field('time');
                                    $time = ltrim($time, '0');
                                    echo $time;
                                    the_sub_field('am/pm');

                                    endwhile;
                                    endif;

                                    echo ' ' . __('–') . ' ';

                                    if (have_rows('time_end')):
                                    while (have_rows('time_end')) : the_row();

                                    $time = get_sub_field('time');
                                    $time = ltrim($time, '0');
                                    echo $time;
                                    the_sub_field('am/pm');

                                    endwhile;
                                    endif;
                                    ?>
                                </div>

                                <?php edit_post_link('[Edit Event]', '<div class="edit-link">', '</div>'); ?>
                                <div class="indent">

                                    <?php

                                    $c = 0;
                                    if (have_rows('talks')):
                                    while (have_rows('talks')) : the_row();

                                    $title = get_sub_field('title');
                                    $subtitle = get_sub_field('subtitle');
                                    $description = get_sub_field('description');
                                    $x = get_sub_field('unique_repeater_id');
                                    ?>

                                    <div class="pretitle"><?=$title;?></div>

                                    <?php if ($subtitle) : ?>

                                        <div class="event-title">
                                            <?php $content = get_sub_field('description'); if ($content) : ?>

                                                  <?php echo $subtitle; ?>
                                                  <br><button onclick="myFunction(myDIV<?php echo $x; ?>)">More Details</button>
                                        </div>

<div id="myDIV<?php echo $x; ?>" style="display: none"><?php $i++ ;?><?php echo $description; ?> <?php else: ?> <?php echo $subtitle; ?> <?php endif; ?> </div>

                                    <?php endif; ?>

                                    <ul class="speaker-list">


                                        <?php
                                        $speakers = get_sub_field('speakers');

                                        if ($speakers) :
                                            foreach ($speakers as $speaker) :

                                            $label = '';
                                            $mod = get_field('moderator', $speaker->ID);
                                            $intr = get_field('intro_speaker', $speaker->ID);

                                            if ($mod == true) :
                                                $label = ' <span style="color:' . get_field('primary_branding_color', 'options') . '">(moderator)</span>';
                                            endif;

                                            if ($intr == true) :
                                                $label = ' <span style="color: #35a6c4">(introduction)</span>';
                                            endif;

                                            ?>

<li>

<a class='iframe' href="<?php echo get_permalink($speaker->ID); ?>"><?php echo get_the_title($speaker->ID); ?></a><?php echo $label; ?>

    <?php the_field('job_title', $speaker->ID); ?>
</li>

                                            <?php
                                            endforeach;
                                        endif; ?>

                                    </ul>

                                    <?php
                                        $c++;
                                    endwhile;
                                    endif;
                                    ?>


                                </div>
                            </li>

                            <?php } endwhile; ?>

                        </ul>

            </section>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文