2013-07-11 3 views
0

Я использую эту функцию на localhost, которая отлично работает, но когда я перехожу на мой сайт, она показывает нуль.Next/Prev link post function not working

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; 
} 

В моей одиночной Post.php:

<?php 
$prev = mod_get_adjacent_post('prev', 'post'); 
$next = mod_get_adjacent_post('next', 'post'); ?> 
<a href="<?php echo get_permalink($prev->ID)?>" id="postprev"><i class="post-prev"></i> Previous Post</a> 
<a href="<?php echo get_permalink($next->ID)?>" id="postnext">Next Post <i class="post-next"></i></a> 

Когда я var_dump($prev) это показывает string(0)"" (NULL). Как это исправить?

+0

Просьба уточнить, что вы подразумеваете под "он показывает нуль." –

+0

В моем одном-post.php: Previous Post Next Post Когда я var_dump (пред $) он показывает строку (0) "" (NULL). Почему он показывает null, когда я переношу его на свой сайт? Действительно спасибо за помощь. –

ответ

1

Вы можете использовать поочередно: get_next_post() get_previous_post().

Например:

<?php $nextpost = get_next_post(); 
$excerpt = $nextpost->post_excerpt ? $nextpost->post_excerpt :apply_filters('get_the_excerpt', $nextpost->post_content); 
echo $excerpt; ?> 

http://codex.wordpress.org/Function_Reference/get_next_post

http://codex.wordpress.org/Function_Reference/get_previous_post

+0

Wow thanks, Он работает хорошо. –

+0

Вы почти всегда рады! Я рад помочь вам! –

+0

Ты спасаешь мою жизнь, действительно спасибо. :) –