Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save celticwebdesign/f45571c2718edbe0ecbf67320d750769 to your computer and use it in GitHub Desktop.
Save celticwebdesign/f45571c2718edbe0ecbf67320d750769 to your computer and use it in GitHub Desktop.
WordPress - get attachment by url parameter and display in page
http://xxx.com/race-results/?series=Attachment_Name_Here.html
if( isset( $_GET['series'] ) ) {
function get_attachment_url_by_slug( $slug ) {
$args = array(
'post_type' => 'attachment',
'name' => $slug,
'posts_per_page' => 1,
'post_status' => 'inherit'
);
$_header = new WP_Query($args);
if( count($_header) > 0 ) {
return $header = wp_get_attachment_url($_header->posts[0]->ID);
} else {
return $header = null;
}
}
$slug = explode(".",$_GET['series']);
$header_url = get_attachment_url_by_slug( $slug[0] );
echo "
<section class='l-section wpb_row height_medium'>
<div class='l-section-h i-cf'>
<div class='sailwave'>";
if( $header_url ) {
$doc = new DOMDocument();
$doc->loadHTMLFile($header_url);
echo $doc->saveHTML();
} else {
echo "Sorry, no results found.";
}
echo "</div>
</div>
</section>
";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment