2012-04-29 29 views

Trả lời

23

Dường như câu hỏi này đã được hỏi trên tất cả các mạng liên kết không có câu trả lời rõ ràng. Vì vậy, tôi đã tạo ra chức năng của riêng mình từ get_adjacent_post gốc và tùy chỉnh nó cho bất kỳ ai khác cần nó.

Chức năng

Drop này trong functions.php của bạn

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

Cách sử dụng

cơ bản sử dụng

// Custom post types can be array() or string 
$post1 = mod_get_adjacent_post('prev', array('post', 'custom1', 'custom2')); 
$post2 = mod_get_adjacent_post('next', 'custom2'); 

Để tạo prev/liên kết tiếp theo

<?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)?>">&laquo; Go back in time</a> 
<?php endif; ?> 

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

Bạn vẫn có thể thay đổi mã nếu bạn vẫn muốn bao gồm các biến $in_same_cat$excluded_categories nhưng nếu bạn làm sau đó tôi đề nghị bạn sử dụng get_adjacent_post thay vì đó là những gì nó cho.

+0

Rất tốt, cảm ơn! Nếu được sử dụng trên bất kỳ loại trang nào, bạn có thể dễ dàng thêm thông số $ thứ ba (hoặc thực tế, đầu tiên) $ để chuyển loại bài đăng trực tiếp vào hàm (đừng quên xóa bài $ toàn cầu sau đó). – JosFabre

+1

cần cập nhật để xử lý biến '$ post_type' chuỗi: nếu (is_array ($ post_types)) { $ txt = ''; cho ($ i = 0; $ i <= số ($ post_types) - 1; $ i ++) { $ txt. = "'". $ Post_types [$ i]. "'"; nếu ($ i! = Đếm ($ post_types) - 1) $ txt. = ','; } $ post_types = $ txt; } else { \t $ post_types = "'" .trim ($ post_types, "'"). "'"; } – emc

+0

cách chúng tôi có thể sửa đổi mã để hoạt động cho các bài đăng trong cùng một danh mục? – CyberJunkie

Các vấn đề liên quan