Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- This is a function customization to get more than one previous and next post!
- Original function can be found at https://core.trac.wordpress.org/browser/tags/4.0/src/wp-includes/link-template.php#L1465
- Put the code below into your theme functions.php file.
- */
- function wpmidia_get_adjacent_posts( $post_id = null, $limit = 1, $previous = true, $in_same_term = false, $excluded_terms = '', $taxonomy = 'category' ) {
- global $wpdb;
- if ( ( ! $post = get_post( $post_id ) ) || ! taxonomy_exists( $taxonomy ) )
- return null;
- $current_post_date = $post->post_date;
- $join = '';
- $posts_in_ex_terms_sql = '';
- if ( $in_same_term || ! empty( $excluded_terms ) ) {
- $join = " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
- if ( $in_same_term ) {
- if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
- return '';
- $term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
- if ( ! $term_array || is_wp_error( $term_array ) )
- return '';
- $join .= $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id IN (" . implode( ',', array_map( 'intval', $term_array ) ) . ")", $taxonomy );
- }
- $posts_in_ex_terms_sql = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
- if ( ! empty( $excluded_terms ) ) {
- if ( ! is_array( $excluded_terms ) ) {
- if ( false !== strpos( $excluded_terms, ' and ' ) ) {
- _deprecated_argument( __FUNCTION__, '3.3', sprintf( __( 'Use commas instead of %s to separate excluded terms.' ), "'and'" ) );
- $excluded_terms = explode( ' and ', $excluded_terms );
- } else {
- $excluded_terms = explode( ',', $excluded_terms );
- }
- }
- $excluded_terms = array_map( 'intval', $excluded_terms );
- if ( ! empty( $term_array ) ) {
- $excluded_terms = array_diff( $excluded_terms, $term_array );
- $posts_in_ex_terms_sql = '';
- }
- if ( ! empty( $excluded_terms ) ) {
- $posts_in_ex_terms_sql = $wpdb->prepare( " AND tt.taxonomy = %s AND tt.term_id NOT IN (" . implode( $excluded_terms, ',' ) . ')', $taxonomy );
- }
- }
- }
- $adjacent = $previous ? 'previous' : 'next';
- $op = $previous ? '<' : '>';
- $order = $previous ? 'DESC' : 'ASC';
- $join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms );
- $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_terms_sql", $current_post_date, $post->post_type), $in_same_term, $excluded_terms );
- $sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT $limit" );
- $query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
- $query_key = 'adjacent_post_' . md5( $query );
- $result = wp_cache_get( $query_key, 'counts' );
- if ( false !== $result ) {
- if ( $result )
- $result = array_map( 'get_post', $result );
- return $result;
- }
- $result = $wpdb->get_col( $query );
- if ( null === $result )
- $result = '';
- wp_cache_set( $query_key, $result, 'counts' );
- if ( $result )
- $result = array_map( 'get_post', $result );
- return $result;
- }
- /*
- Usage: ATTENTION: THIS IS A SAMPLE CODE! CHANGE IT FOR YOUR PURPOSE...
- Put the code below where you want to show de list of posts
- TIP: You can use print_r() to see what the function is bringing.....
- PARAMTERS:
- $post_id = The ID of the actual post
- $limit = Number of posts you want
- $previous = If true, it shows the previous posts list. If false, it shows the next posts list.
- ...
- */
- echo '<h2>Previous posts</h2>';
- $previous = wpmidia_get_adjacent_posts(19, 3); // CHANGE THE ID AND NUMBER OF POSTS
- if( $previous ){
- echo '<ul>';
- foreach( $previous as $prev ){
- echo '<li>[' . mysql2date('d/m/Y', $prev->post_date) . '] ' . $prev->post_title . '</li>';
- }
- echo '</ul>';
- }
- echo '<h2>Next posts</h2>';
- $next = wpmidia_get_adjacent_posts(19, 3, false); // CHANGE THE ID AND NUMBER OF POSTS
- if( $next ){
- echo '<ul>';
- foreach( $next as $nextpost ){
- echo '<li>[' . mysql2date('d/m/Y', $nextpost->post_date) . '] ' . $nextpost->post_title . '</li>';
- }
- echo '</ul>';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement