Skip to content

Instantly share code, notes, and snippets.

@Garconis
Created January 11, 2022 22:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Garconis/569a344ca41362399d9d443affb23b12 to your computer and use it in GitHub Desktop.
Save Garconis/569a344ca41362399d9d443affb23b12 to your computer and use it in GitHub Desktop.
WordPress | Previous and next shortcode for post navigation on single posts
<?php
add_shortcode( 'fs_prev_next_thumbs', 'generate_faq_landing_link_shortcode' );
function generate_faq_landing_link_shortcode( $atts, $content ) {
global $post; // if outside the loop
$prev_next = '';
$prev_post = get_previous_post();
$next_post = get_next_post();
// wrapper opening class
if($prev_post && $next_post) {
$prev_next_wrapper_class = 'fs-post-nav-thumb-both';;
}
else {
$prev_next_wrapper_class = 'fs-post-nav-thumb-single';
}
// wrapper opening
$prev_next .= '<div class="fs-post-nav-thumb-container ' . $prev_next_wrapper_class . '">';
if($prev_post) {
$prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
$prev_featured_img_url = get_the_post_thumbnail_url($prev_post->ID,'full');
$prev_next .= '<div class="fs-post-nav-thumb"><a rel="prev" href="' . get_permalink($prev_post->ID) . '" title="' . $prev_title. '" style="background-image:url('. $prev_featured_img_url .'); background-position: center; background-size: cover;"><div><p class="fs-thumb-prev">Prev</p><p class="fs-thumb-title">' . $prev_title . '</p><p class="fs-thumb-excerpt">' . get_the_excerpt($prev_post->ID) . '</p></div></a></div>';
}
if($next_post) {
$next_title = strip_tags(str_replace('"', '', $next_post->post_title));
$next_featured_img_url = get_the_post_thumbnail_url($next_post->ID,'full');
$prev_next .= '<div class="fs-post-nav-thumb"><a rel="next" href="' . get_permalink($next_post->ID) . '" title="' . $next_title. '" style="background-image:url('. $next_featured_img_url .'); background-position: center; background-size: cover;"><div><p class="fs-thumb-next">Next</p><p class="fs-thumb-title">' . $next_title . '</p><p class="fs-thumb-excerpt">' . get_the_excerpt($next_post->ID) . '</p></div></a></div>';
}
// wrapper closing
$prev_next .= '</div>';
// begin output buffering
ob_start();
echo $prev_next;
//get the buffer contents
$content = ob_get_contents();
//end the output buffer
ob_end_clean();
return $content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment