简体   繁体   English

获取ACF中的页面ID

[英]Get ID of Page in ACF

I have a very simple loop in which I want to display some posters, and once clicked to show the popup with the related product. 我有一个非常简单的循环,我想在其中显示一些海报,并单击以显示相关产品的弹出窗口。 I have created this with a custom post type and with ACF Page Link as field type. 我已经使用自定义帖子类型和ACF页面链接作为字段类型创建了此页面。

在此处输入图片说明

Now After having created this ACF field for my custom Post type I can select my desired Product (for which I will need the ID) from a Metabox 现在,在为我的自定义帖子类型创建此ACF字段后,我可以从Metabox中选择所需的产品(需要ID)

在此处输入图片说明

My current loop 我当前的循环

    <ul class="products columns-5">
        <?php $args = array('post_type' => 'posters'); ?>
        <?php $loop = new WP_Query($args); ?>
        <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <li class="product" style="width: 330px;">
           <div class="product-inner clearfix">
              <div class="mf-product-thumbnail">
                 <a href="<?php the_field('my_link'); ?>" data-id="<?php echo get_permalink($postID); ?>" class="mf-product-quick-view" tabindex="0"><img width="300" height="450" src="<?php echo get_the_post_thumbnail_url($post_id, 'full'); ?>" class="" alt=""></a>
              </div>
           </div>
        </li>
        <?php endwhile; ?>
        <?php else: ?>
        <?php endif; ?>
        <?php wp_reset_postdata(); ?>
    </ul>

While the a href link works using a custom field, the problem I am currently trying to solve is to get the actual ID of the product as it seems to be required to work with data-id . 虽然href链接使用自定义字段工作,但我目前正在尝试解决的问题是获取产品的实际ID,因为似乎需要使用data-id I've tried to add get_permalink($postID); 我试图添加get_permalink($ postID); however this does not seem to be working. 但是,这似乎不起作用。 Some expert advice would be greatly appreciated, thank you. 一些专家的建议将不胜感激,谢谢。

Just use current loop element ID.(default WordPress post object id) 只需使用当前循环元素ID。(默认的WordPress post对象ID)
Code should look like: 代码应如下所示:

<ul class="products columns-5">
        <?php $args = array('post_type' => 'posters'); ?>
        <?php $loop = new WP_Query($args); ?>
        <?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); 
 $id = $loop->post->ID;
?>
        <li class="product" style="width: 330px;">
           <div class="product-inner clearfix">
              <div class="mf-product-thumbnail">
                 <a href="<?php the_field('my_link'); ?>" data-id="<?php echo $id; ?>" class="mf-product-quick-view" tabindex="0"><img width="300" height="450" src="<?php echo get_the_post_thumbnail_url($post_id, 'full'); ?>" class="" alt=""></a>
          </div>
       </div>
    </li>
    <?php endwhile; ?>
    <?php else: ?>
    <?php endif; ?>
    <?php wp_reset_postdata(); ?>
</ul>

UPDATED: 更新:
To get linked product link and ID just change your ACF field type to Post Object, set filter by post type(for product -> post type product ) and set return type to Post object, after that do like -> 要获得链接的产品链接和ID,只需将您的ACF字段类型更改为Post Object,按发布类型(对于product-> post type product )设置过滤器,然后将返回类型设置为Post object,然后执行->

$linked_product = get_field('my_link');
$id = $linked_product->$post_id;
$link = $linked_product-> (i dont know what object returned exactly just do "var_dump($linked_product)" to chek what is inside).

And after that change href and data-id attributed content 之后,更改href和data-id属性内容 ACF字段设置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM