Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active December 6, 2018 13:36
Show Gist options
  • Save jchristopher/8597678 to your computer and use it in GitHub Desktop.
Save jchristopher/8597678 to your computer and use it in GitHub Desktop.
Modify a WordPress search results template to directly link to PDF files included in results (see Revisions for changes made)
<?php global $post; ?>
<!-- Start the Loop. -->
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<!--post Start-->
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
// our default will be the post permalink
$permalink = get_permalink();
// the default excerpt will be the post excerpt
$excerpt = get_the_excerpt();
// check to see if the result is an attachment
if( 'attachment' == get_post_type() ) {
// check to see if the result is a PDF
if( 'application/pdf' == get_post_mime_type( $post->ID ) ) {
// override the permalink
$permalink = wp_get_attachment_url( $post->ID );
// override the excerpt
$excerpt = get_post_meta( $post->ID, 'searchwp_content', true );
$excerpt_char_length = 250; // change this to be how many characters you want in the excerpt
$excerpt = substr( $excerpt, 0, $excerpt_char_length ) . '[...]';
}
}
?>
<h1 class="post_title"><a href="<?php echo $permalink; ?>" rel="bookmark" title="<?php printf(esc_attr__('Permalink to %s', 'andrina-lite'), the_title_attribute('echo=0')); ?>"><?php the_title(); ?></a></h1>
<div class="post_content"><?php if (has_post_thumbnail()) { ?>
<a href="<?php echo $permalink; ?>">
<?php the_post_thumbnail('post_thumbnail', array('class' => 'postimg')); ?>
</a>
<?php } else { ?>
<a href="<?php echo $permalink; ?>"><?php echo inkthemes_main_image(250, 170); ?></a>
<?php
}
?>
<?php echo $excerpt; ?>
<div class="clear"></div>
<?php if (has_tag()) { ?>
<div class="tag">
<?php the_tags(__('Post Tagged with ', ', ', '')); ?>
</div>
<?php } ?>
<ul class="post_meta">
<li class="post_date"><?php the_date(); ?></li>
<li class="post_category"><span>/&nbsp;&nbsp;<?php _e('Category', 'andrina-lite'); ?></span>&nbsp;<?php the_category(', '); ?></li>
</ul>
<a class="read_more" href="<?php echo $permalink; ?>"><?php _e('Read More', 'andrina-lite'); ?></a> </div>
</div>
<!--post End-->
<?php
endwhile;
else:
?>
<div class="post">
<p>
<?php _e('Sorry, no posts matched your criteria.', 'andrina-lite'); ?>
</p>
</div>
<?php endif; ?>
<!--End Loop-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment