Skip to content

Instantly share code, notes, and snippets.

@magnificode
Last active August 29, 2015 14:13
Show Gist options
  • Save magnificode/f0d7c7834ea5eee6ee2c to your computer and use it in GitHub Desktop.
Save magnificode/f0d7c7834ea5eee6ee2c to your computer and use it in GitHub Desktop.
Snippet that detects if a given ACF Page URL is an attachment page, detects the ID of that attachment page, and returns the URL for the actual attachment.
<?php
$ctaLink = get_field('static_hero_cta_button_link');
if (false !== strpos($ctaLink,'attachment_id')) {
$url = $ctaLink;
$url_parsed = parse_url($url);
parse_str($url_parsed['query'], $url_parts);
$attachmentID = $url_parts['attachment_id']; // attachment ID
$attachmentSrc = wp_get_attachment_url( $attachmentID ); // returns an array
$ctaLink = $attachmentSrc;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment