PHP Wordpress ACF页面链接


PHP Wordpress ACF Page Link

我正在尝试使用ACF(高级自定义字段)插件中继器字段链接到其他wordpress页面。我在我的front_page.php中有这段代码:

// Product Grid Repeater
  $grid = get_post_meta( get_the_ID(), 'product_grid', true ); 
  // Check if acf/custom field data exists
  if( $grid ) {
    ?>
    <div class="product-grid text-center">
        <h3>Our Product Lineup</h3>
     <ul id="productgrid" class="large-block-grid-4 medium-block-grid-2 small-block-grid-2 effect-2" data-equalizer>
    <?php
    // loop through the rows of data
    for( $i = 0; $i < $grid; $i++ ) { // Custom Count/Loop through rows
    // Important: Notice the _preceding and trailing_ underscores
    // for $hero_image Set image return value to array and use a variable to test if photon works by using echo $image['url']
    $image = (int) get_post_meta( get_the_ID(), 'product_grid_' . $i . '_product_image', true ); // Subfield Name: gallery_slide_image, Type: Image
    $name = get_post_meta( get_the_ID(), 'product_grid_' . $i . '_product_name', true ); // Subfield Name: gallery_slide_title, Type: Text
    $type = get_post_meta( get_the_ID(), 'product_grid_' . $i . '_product_type', true ); // Subfield Name: gallery_slide_title, Type: Text
    $link = esc_html( get_post_meta( get_the_ID(), 'product_grid_' . $i . '_product_link', true ) ); // Subfield Name: gallery_slide_link, Type: Page Link 
    ?>

            <li>
              <a href="<?php echo $link; ?>">
              <div class="grid-outline" data-equalizer-watch>
                  <div class="product-grid-img-wrap">
                    <?php echo wp_get_attachment_image( $image, 'full' ); ?>
                  </div>
                  <span class="type"><?php echo $type ?></span>
              </div>
              </a>
            </li>
      <?php
    } // CLOSE for
    ?>
    </ul>
    </div>
    <?php
  } //CLOSE if( $grid )

,但当我去点击链接,他们去他们的ID,而不是URL/永久链接。我有一种感觉,它是与$link = esc_html( get_post_meta( get_the_ID(), 'product_grid_' . $i . '_product_link', true ) ); // Subfield Name: gallery_slide_link, Type: Page Link部分,但我真的不知道该怎么改变它。

我不知道PHP,因为我正在接管一个离开公司的设计师的网站-现在我是唯一的设计师!提前感谢您的帮助!

这个问题很老了,我来自一些"ACF Page Link"谷歌搜索,同时寻找一个不同的问题,只是想在这里添加一个提示。

首先,页面链接字段在wp_postmeta表中存储页面/帖子ID,因此当您使用本机wp get_post_meta()功能时,您总是获得ID,而不是您可能期望的URL。如果您使用ACF函数get_field(),您将获得URL。

因此,解决方案要么使用get_field(),要么完全使用wordpress的方式,使用get_permalink( get_post_meta( 'field_name' ) )

修复方法:

<?php echo $link[url]; ?>

链接作为数组工作,因为你可以使用它的标题等。

要从中继器子字段获取post ID,请使用get_sub_field函数和format中的false选择器。

if ( have_rows( 'repeater' ) ):
  while ( have_rows( 'repeater' ) ):the_row();
    $post_id=get_sub_field( 'page_link_field', false );
  endwhile;
endif;