Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:07
What would you like to do?
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(); ?>>
$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 );
<a href="<?php echo esc_url( $permalink ); ?>"><?php the_title(); ?></a>
<?php the_excerpt(); ?>
<?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