0

I'm trying to recieve post ID from link using ACF link field:

<?php 
  $link = get_sub_field('offer_link');
  $id = get_the_ID();
    if( $link ): ?>
    <a href="#post-<?php echo $id; ?>" target="<?php echo $link['target']; ?>"><?php echo $link['title']; ?></a>
<?php endif; ?>

but instead of ID of link I get the ID of current page. How should it be done?

0

The Page Link documentation actually shows how to retrieve the ID from a Page Link field. Just needed to do this myself and came across it. It worked well for me.

<?php 

// vars
$post_id = get_field('url', false, false);

// check 
if( $post_id ): ?>
<a href="<?php echo get_the_permalink($post_id); ?>"><?php echo get_the_title($post_id); ?></a>
<?php endif; ?>

The "false, false" parameters allow you to retrieve more details.

| improve this answer | |
3

You can get a post's ID from a URL with url_to_postid(). Just pass it the URL to get the ID back:

$link = get_sub_field( 'offer_link' );
$id = url_to_postid( $link['url'] );

But if you want a field that will give you an ID of a selected page you should use probably use the Relationship field instead. It'd probably be much more efficient. It also means that if the URL to that post changes your saved data wouldn't cease to work like it would if you were saving the URL.

| improve this answer | |
  • I have one page theme and I need add link to one of pages, so I need link to specific ID. Should I use Relationship field for that? – Damian May 31 '18 at 16:23
  • If you don't want the user to have to enter the ID, then yes. – Jacob Peattie Jun 1 '18 at 1:08
1

I don't think that you should use the Link field in your case, if you want to create relation with post object...

It would be much better to use Relation field, if you should be able to select many posts, or just Post Object, if it should be possible to select only one value.

If you go for the Post Object field type, then remember to set its Return Format to Post ID.

This way get_sub_field( 'your-field-name' ) will return exactly the ID of selected post.

If you want to display link to this post, you can always use echo get_permalink( get_sub_field( 'your-field-name' ) );

So this is your code after modifications (I've used your original name for the field, but it would be nice to change it too, I guess, since it doesn't contain link anymore):

<?php 
    $post_id = get_sub_field('offer_link');
    if ( $post_id ):
?>
<a href="#post-<?php echo esc_attr($post_id); ?>"><?php echo get_post_field( 'post_title', $post_id ); ?></a>
<?php endif; ?>
| improve this answer | |
-1

This works form me:

<?php 

$link = get_sub_field('offer_link');
$x = $link->ID;

if( $link ): ?>
<a href="#post-<?php echo $x ?>"><?php echo get_sub_field('offer_link_text'); ?></a>
<?php endif; ?>

and Post Object field.

Thaks for everyone for help :)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.