Last active
September 7, 2020 17:03
-
-
Save hieuhuynh93/ecbd1e60facdfe4af44d02ca6fe64f81 to your computer and use it in GitHub Desktop.
Lấy bài viết next/previous wordpress - cakedeli
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 | |
function install_next_post_sc(){ | |
$class = 'next_post_'; | |
global $post; | |
$categories = get_the_category($post->ID); | |
if(!empty($categories)){ | |
$category = $categories[0]->term_id; | |
} else { | |
$category = ''; | |
} | |
if( get_adjacent_post(true, '', false) ) { | |
ob_start(); | |
next_post_link( '<div class="'.$class.'">%link</div>', '%title <i class="icon-angle-right"></i>', true); | |
$result = ob_get_contents(); | |
ob_end_clean(); | |
return $result; | |
} else { | |
ob_start(); | |
$last = new WP_Query('page__not_in='.$post->ID.'&cat='.$category.'&posts_per_page=1&order=ASC');$last->the_post(); | |
echo '<span class="'.$class.'"><a href="' . get_permalink() . '">' . get_the_title() . ' <i class="icon-angle-right"></i> </a></span>'; | |
wp_reset_query(); | |
$result = ob_get_contents(); | |
ob_end_clean(); | |
return $result; | |
}; | |
} | |
add_shortcode( 'next_post_sc', 'install_next_post_sc' ); | |
function install_previous_post_sc(){ | |
$class = 'previous_post_'; | |
global $post; | |
$categories = get_the_category($post->ID); | |
if(!empty($categories)){ | |
$category = $categories[0]->term_id; | |
} else { | |
$category = ''; | |
} | |
if( get_adjacent_post(true, '', true) ) { | |
ob_start(); | |
previous_post_link( '<div class="'.$class.'"><i class="icon-angle-left"></i> %link</div>', '%title', true ); | |
$result = ob_get_contents(); | |
ob_end_clean(); | |
return $result; | |
} else { | |
ob_start(); | |
$first = new WP_Query('page__not_in='.$post->ID.'&cat='.$category.'&posts_per_page=1&order=DESC'); $first->the_post(); | |
echo '<div class="'.$class.'"><a href="' . get_permalink() . '"><i class="icon-angle-left"></i> ' . get_the_title() . '</a></div>'; | |
wp_reset_query(); | |
$result = ob_get_contents(); | |
ob_end_clean(); | |
return $result; | |
} | |
} | |
add_shortcode( 'previous_post_sc', 'install_previous_post_sc' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment