Skip to content

Instantly share code, notes, and snippets.

Embed
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(); ?>>
<?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
You can’t perform that action at this time.