2012-03-29 54 views
5

我正在開發一個wordpress網站,其中包含在循環中按字母順序排列的自定義帖子。WordPress的:previous_post_link/next_post_link按字母順序?

<!-- THE ARGS --> 
<?php global $query_string; 
$args = wp_parse_args($query_string); 
$args = array(
    'post_type' => 'custom_post', 
    'orderby' => 'title', 
    'order' => 'ASC', 
    'posts_per_page' => -1, 
    ); ?> 

<!-- THE LOOP --> 
<?php $wp_query = new WP_Query($args); ?> 
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?> 
<?php endwhile; ?> 
<?php wp_reset_query(); ?> 

沒有什麼奇特的,只是一個正常的循環。

當我打開一個帖子,在其頁面中,我有通常的previous_post_link和next_post_link,所以我可以瀏覽帖子。但是,這是按照帖子的時間順序完成的,我希望按照我在循環中使用的相同字母順序來完成,當然。任何有關此事的幫助將非常感激。 在此先感謝!

回答

6

看起來this plug-in做你以後,例如:

<ul class="pager"> 
     <?php previous_post_link_plus(array('order_by' => 'post_title')); ?> 
     <?php next_post_link_plus(array('order_by' => 'post_title')); ?> 
</ul> 
+0

這是非常好的。非常感謝,夥伴。 – Cthulhu 2012-06-26 12:18:30

+0

這個插件在幾年內還沒有更新,但它仍然很棒!有人應該再次測試它,以便它可以回到wp admin中的可安裝選項。我不得不重新從wp網站上傳它。 – 2016-08-23 22:42:25

0

這種插件還沒有在年更新一次,不知道這是否仍然有效。如果沒有的話,I came up with a solution可以添加到你的functions.php中。希望能幫助到你!

3

您可以使用get_adjacent_post函數中的過濾器來完成此操作。

在你的functions.php文件,添加:

function mytheme_previous_post_orderby_name($orderby){ 
    return "ORDER BY p.post_title DESC LIMIT 1"; 
} 
function mytheme_previous_post_where_name(){ 
    global $post, $wpdb; 
    return $wpdb->prepare("WHERE p.post_title < %s AND p.post_type = %s AND (p.post_status = 'publish' OR p.post_status = 'private')", $post->post_title, $post->post_type); 
} 
function mytheme_next_post_orderby_name($orderby){ 
    return "ORDER BY p.post_title ASC LIMIT 1"; 
} 
function mytheme_next_post_where_name(){ 
    global $post, $wpdb; 
    return $wpdb->prepare("WHERE p.post_title > %s AND p.post_type = %s AND (p.post_status = 'publish' OR p.post_status = 'private')", $post->post_title, $post->post_type); 
} 

然後在你的single.php頁面中添加過濾器,你打電話之前上一個/下一個帖子鏈接功能:

add_filter('get_previous_post_sort', 'mytheme_previous_post_orderby_name', 10, 1); 
add_filter('get_next_post_sort', 'mytheme_next_post_orderby_name', 10, 1); 

add_filter('get_previous_post_where', 'mytheme_previous_post_where_name', 10); 
add_filter('get_next_post_where', 'mytheme_next_post_where_name', 10); 

the_post_navigation(); 

remove_filter('get_previous_post_sort', 'mytheme_previous_post_orderby_name', 10); 
remove_filter('get_next_post_sort', 'mytheme_next_post_orderby_name', 10); 
remove_filter('get_previous_post_where', 'mytheme_previous_post_where_name', 10); 
remove_filter('get_next_post_where', 'mytheme_next_post_where_name', 10); 

如果您想要檢查您的具體post_type,您可以添加if如果在添加過濾器部分:

if($post->post_type == 'my_custom_post_type'){ 
    add_filter(...); 
    the_post_navigation(); 
    remove_filter(...); 
} 

或者,你可以使用一個post_type特定的single.php文件!

這對我的工作很好,但可能有一定的侷限性,如果您打算在此與同期限的職位相結合......

+1

Danbrellis的答案有一個缺失的撇號。我不能評論,因爲這是我的第一個帳戶,並沒有足夠的代表評論。 remove_filter('get_previous_post_sort',mytheme_previous_post_orderby_name',10);應該是'remove_filter('get_previous_post_sort','mytheme_previous_post_orderby_name',10);'(但是即使有了這個修復,我也無法得到這個工作) – plumbinator 2015-10-21 04:17:17

+0

plumbinator-謝謝你。你靠近嗎?如果你有一些示例代碼,我很高興看一看。 – danbrellis 2015-10-21 17:52:04