Search
 
SCRIPT & CODE EXAMPLE
 

PHP

get_adjacent_post wordpress

$next_post = get_adjacent_post(0, '', 0);
if( $next_post )
	echo '<a href="'. get_permalink($next_post->ID) .'">'. $next_post->post_title .'</a>';
Comment

get_adjacent_post wordpress

$prev_post = get_adjacent_post();
if( $prev_post )
	echo '<a href="'. get_permalink($prev_post->ID) .'">'. $prev_post->post_title .'</a>';
Comment

get_adjacent_post wordpress

/*
 * Replacement for get_adjacent_post()
 *
 * This supports only the custom post types you identify and does not
 * look at categories anymore. This allows you to go from one custom post type
 * to another which was not possible with the default get_adjacent_post().
 * Orig: wp-includes/link-template.php 
 * 
 * @param string $direction: Can be either 'prev' or 'next'
 * @param multi $post_types: Can be a string or an array of strings
 */
function mod_get_adjacent_post($direction = 'prev', $post_types = 'post') {
    global $post, $wpdb;

    if(empty($post)) return NULL;
    if(!$post_types) return NULL;

    if(is_array($post_types)){
        $txt = '';
        for($i = 0; $i <= count($post_types) - 1; $i++){
            $txt .= "'".$post_types[$i]."'";
            if($i != count($post_types) - 1) $txt .= ', ';
        }
        $post_types = $txt;
    }

    $current_post_date = $post->post_date;

    $join = '';
    $in_same_cat = FALSE;
    $excluded_categories = '';
    $adjacent = $direction == 'prev' ? 'previous' : 'next';
    $op = $direction == 'prev' ? '<' : '>';
    $order = $direction == 'prev' ? 'DESC' : 'ASC';

    $join  = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_cat, $excluded_categories );
    $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type IN({$post_types}) AND p.post_status = 'publish'", $current_post_date), $in_same_cat, $excluded_categories );
    $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" );

    $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort";
    $query_key = 'adjacent_post_' . md5($query);
    $result = wp_cache_get($query_key, 'counts');
    if ( false !== $result )
        return $result;

    $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort");
    if ( null === $result )
        $result = '';

    wp_cache_set($query_key, $result, 'counts');
    return $result;
}
Comment

get_adjacent_post wordpress

<?php
$prev = mod_get_adjacent_post('prev', array('post', 'custom1', 'custom2'));
$next = mod_get_adjacent_post('next', array('post', 'custom1', 'custom2'));
?>

<?php if($prev) : ?>
    <a href="<?php echo get_permalink($prev->ID)?>">« Go back in time</a>
<?php endif; ?>

<?php if($next) : ?>
    <a href="<?php echo get_permalink($next->ID)?>">Next: <?php echo $next->post_title; ?> »</a>
<?php endif; ?>
Comment

PREVIOUS NEXT
Code Example
Php :: acf sub_field image title 
Php :: localhost redirected you too many times. php 
Php :: how to check path laravel 
Php :: symfony rabbitMQ 
Php :: create table laravel 
Php :: convert png to webp in php 
Php :: start php cli 
Php :: get date to current week last or first day dates 
Php :: php pdo setting error modes 
Php :: laravel blade php variable concatenate javascript variable 
Php :: laravel bd query 
Php :: php if negative then 0 
Php :: php function to remove null or 0 value from array 
Php :: laravel validation required if 
Php :: laravel exclude field 
Php :: eloquent insert 
Php :: yii2 gridview action change urls 
Php :: return message in laravel 
Php :: wordpress limit post content length 
Php :: laravel seeder 
Php :: laravel if else condition in blade file 
Php :: php integer 
Php :: laravel array in lang 
Php :: spaceship operator php 
Php :: wordpress get post date custom format 
Php :: laravel eloquent update multiple records 
Php :: change verify email template laravel 
Php :: execute script php command line 
Php :: laravel resource api 
Php :: php prepared statements 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =