If a PDF is included in search results, link directly to the file instead of the attachment page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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