Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/7e0ad4dc4d78bbd03173 to your computer and use it in GitHub Desktop.
Save jchristopher/7e0ad4dc4d78bbd03173 to your computer and use it in GitHub Desktop.
If a PDF is included in search results, link directly to the file instead of the attachment page
<?php if ( have_posts()) : while (have_posts() ) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
$permalink = get_permalink();
if( 'attachment' == get_post_type() ) {
// if the result is a PDF, link directly to the file not the attachment page
$permalink = wp_get_attachment_url( $post->ID );
}
?>
<h2>
<a href="<?php echo esc_url( $permalink ); ?>"><?php the_title(); ?></a>
</h2>
<?php the_excerpt(); ?>
</div>
<?php endwhile; else : ?>
<p>There were no posts found.</p>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment