2015-10-01 59 views
-1

議決下一頁/一篇文章鏈接訂購

我窮盡搜索提供下一個方法和上一篇文章中從它通常出現在單以不同的方式鏈接發佈

默認情況下它:

  • 是按時間順序排列

  • 鏈接所有博客類別

的職位,但我需要它:

  • 字母順序排列的

  • 來自同一類別鏈接到帖子僅

我不是開發人員,但我發現了兩個碼,我想如果我能合併這兩個問題將得到解決。請有人幫助我嗎?

CODE 1 - 打開下一個/後退鏈接alphabetcally,但不能從相同類別(source

function filter_next_post_sort($sort) { 
    $sort = "ORDER BY p.post_title ASC LIMIT 1"; 
    return $sort; 
} 
function filter_next_post_where($where) { 
    global $post, $wpdb; 
    return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title); 
} 

function filter_previous_post_sort($sort) { 
    $sort = "ORDER BY p.post_title DESC LIMIT 1"; 
    return $sort; 
} 
function filter_previous_post_where($where) { 
    global $post, $wpdb; 
    return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title); 
} 

add_filter('get_next_post_sort', 'filter_next_post_sort'); 
add_filter('get_next_post_where', 'filter_next_post_where'); 

add_filter('get_previous_post_sort', 'filter_previous_post_sort'); 
add_filter('get_previous_post_where', 'filter_previous_post_where'); 

CODE 2 - 關閉來自相同類別接着/後退的鏈接,但是不按字母順序(source

add_filter('get_next_post_join', 'navigate_in_same_taxonomy_join', 20); 
add_filter('get_previous_post_join', 'navigate_in_same_taxonomy_join', 20); 
function navigate_in_same_taxonomy_join() { 
global $wpdb; 
return " 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"; 
} 


add_filter('get_next_post_where' , 'navigate_in_same_taxonomy_where'); 
add_filter('get_previous_post_where' , 'navigate_in_same_taxonomy_where'); 
function navigate_in_same_taxonomy_where($original) { 
global $wpdb, $post; 
$where = ''; 
$taxonomy = 'category'; 
$op = ('get_previous_post_where' == current_filter()) ? '<' : '>'; 
$where = $wpdb->prepare("AND tt.taxonomy = %s", $taxonomy); 
if (! is_object_in_taxonomy($post->post_type, $taxonomy)) 
return $original ; 

$term_array = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'ids')); 

$term_array = array_map('intval', $term_array); 

if (! $term_array || is_wp_error($term_array)) 
return $original ; 

$where = " AND tt.term_id IN (" . implode(',', $term_array) . ")"; 
return $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_date, $post->post_type); 
} 

經過數週尋找解決方案,這裏是FINAL ANSWER

謝謝你的幫助!

回答

0

您應該使用get_adjacent_post();這是爲您提供的下一個或以前的帖子。

這是以前的帖子:

<?php 
    $prev_post = get_adjacent_post(true, '', true, 'taxonomy_slug'); ?> 
    if (is_a($prev_post, 'WP_Post')) { 
?> 

<a href="<?php echo get_permalink($prev_post->ID); ?>"><?php echo get_the_title($prev_post->ID); ?></a> 

<?php } ?> 

這是下一篇:

<?php 
    $next_post = get_adjacent_post(true, '', false, 'taxonomy_slug'); 
    if (is_a($next_post, 'WP_Post')) { 
?> 

<a href="<?php echo get_permalink($next_post->ID); ?>"><?php echo get_the_title($next_post->ID); ?></a> 

<?php } ?> 
+0

夥計們,我很高興你的反應,但這種方式下/上一個鏈接不工作按字母順序排列。我發現了新的信息,我想可以幫助我,但我不是開發人員,所以我不知道該怎麼做。 我正在用新信息更新我的問題。請閱讀,如果你可以。 再次感謝! – Dormiu

相關問題