Skip to content

Instantly share code, notes, and snippets.

@pommiegranit
Last active April 14, 2017 20:44
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pommiegranit/8762674 to your computer and use it in GitHub Desktop.
Save pommiegranit/8762674 to your computer and use it in GitHub Desktop.
Provides a WordPress template tag that outputs a much more eye-catching "next" post navigation
<?php
/**
* Plugin Name: Supercharged Post Navigation
* Plugin URI: http://premium.wpmudev.org/blog
* Description: Provides a much enhanced post navigation via supercharged_post_navigation function
* Version: 1.0
* Author: Chris Knowles
* Author URI: http://premium.wpmudev.org/blog/author/chrisdknowles
* License: GPL2
*/
/**
* template function
* parameters:
* $title_text : text to show at the top of the navigation element, default: '' (this results in 'NEXT IN [CATEGORY NAME]' being displayed)
* $image_size : which image size to use as the background, default: 'post_thumbnail' (featured image)
* $in_same_term : search first for next post in the same category? default: true
* $excluded_terms : categories to ignore when getting the next post, default: '' (none)
* $previous : search for previous (older) posts, default: true
*/
function supercharged_post_navigation( $title_text='' , $image_size='post_thumbnail' , $in_same_term=true, $excluded_terms='', $previous=true) {
// get the next post in same category
$next_post = get_adjacent_post( $in_same_term, $excluded_terms, $previous);
// nothing found? try without category
if ( !$next_post ) {
$in_same_term = false; // get post regardless of category
$next_post = get_adjacent_post( $in_same_term, $excluded_terms, $previous);
}
//got a post!
if ($next_post) {
// get the featured image
$thumb_id = get_post_thumbnail_id($next_post->ID);
$thumb_url = wp_get_attachment_image_src($thumb_id, $image_size , true);
// get the excerpt - IMPORTANT: You must use either the excerpt metabox or the <!--more--> tag in your body content
if ( has_excerpt( $next_post->ID ) ) {
$the_excerpt = $next_post->post_excerpt;
} else {
$the_excerpt = substr( $next_post->post_content, 0, strpos( $next_post->post_content, '<!--more-->' ) );
}
// if no title text provided then get the category
if ($title_text=='') {
$category = get_the_category();
$title_text = 'next in ' . $category[0]->cat_name;
}
echo '
<div class="next-post-preview" style="padding: 30px; opacity:0.7; background-image: url(' . $thumb_url[0] . ')">
<a href="' . get_permalink( $next_post->ID ) . '">
<div class="next-post-preview-content" style="width: 80%; margin: auto; background-color: #000; opacity: 0.7; padding: 20px; ">
<div style="color: #fff; width: 100%; border-top: 2px solid #fff; margin-top: 20px; padding-top: 5px; text-transform: uppercase">' . $title_text . '</div>
<h2 style="color: #fff;">' . $next_post->post_title . '</h2>
<div style="color: #fff; font-weight: bold;">' . apply_filters( 'get_the_excerpt' , $the_excerpt ) . '</div>
</div>
</a>
</div>';
}
} // end function
@vdite
Copy link

vdite commented Feb 2, 2014

why do you use
apply_filters( 'the_content' , $the_excerpt )
and not just $the_excerpt?
This does kill http://wordpress.org/plugins/2-click-socialmedia-buttons/

@pommiegranit
Copy link
Author

Because I don't know whether the excerpt has come from post_excerpt or from post_content and the latter would perhaps need those filters applied (shortcodes, etc).

But it's probably being overly cautious. Should be safe to remove if you've got conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment