'PHP Wordpress ACF Page Link

I am trying to link to other wordpress pages using the ACF (advance custom field) plugin repeater field. I have this code in my 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 )

but when i go to click on the links they go to their ID and not URL/permalink. I have a feeling it is with the $link = esc_html( get_post_meta( get_the_ID(), 'product_grid_' . $i . '_product_link', true ) ); // Subfield Name: gallery_slide_link, Type: Page Link section but I really am not sure what to change it to.

I do not know PHP as I am taking over a website for a designer who left the company - and now I am the only designer! Thanks in advance for your help!



Solution 1:[1]

The question is pretty old, I came from some "ACF Page Link" google search while looking for a different problem and just want to add a tip here.

First of all, Page Link field stores page/post IDs in the wp_postmeta table, so when you use native wp get_post_meta() function, you always get the ID, not URL as you might expect. If you use ACF function get_field() you will get the URL.

So, the solution is either to use get_field() or go completely WordPress-way and use get_permalink( get_post_meta( 'field_name' ) ).

Solution 2:[2]

Here is the fix:

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

The links are working as arrays because you can use its title etc. I hope it helps you.

Solution 3:[3]

For anyone else looking at this, to get the post ID from a repeater subfield, use the get_sub_field function with a false selector in format.

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ihor Vorotnov
Solution 2 inanc demir
Solution 3 DharmanBot