2012-03-29 14 views
5

Sto sviluppando un sito web di wordpress con post personalizzati che sono ordinati alfabeticamente nel ciclo.Wordpress: previous_post_link/next_post_link in ordine alfabetico?

<!-- 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(); ?> 

Niente di eccezionale, solo un normale ciclo.

Quando apro un post, nella sua pagina ho il solito previous_post_link e next_post_link così posso navigare tra i post. Tuttavia, questo viene fatto in ordine cronologico dei post, e desidero farlo con lo stesso ordine alfabetico che ho usato nel ciclo, ovviamente. Qualsiasi aiuto in merito sarebbe molto apprezzato. Grazie in anticipo!

risposta

6

Sembra this plug-in fa quello che stai dopo, ad es .:

<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

Questo è eccellente. Grazie mille, amico. – Cthulhu

+0

Questo plugin non è stato aggiornato tra un paio d'anni, ma funziona ancora alla grande !!! Qualcuno dovrebbe testarlo nuovamente in modo che possa tornare alle opzioni installabili nell'amministratore di wp. Ho dovuto ricaricarlo dal sito wp. –

0

Questo plug-in non è stato aggiornato negli anni, non so se funziona ancora. In caso contrario, I came up with a solution che può essere aggiunto al tuo functions.php. Spero che sia d'aiuto!

3

È possibile farlo utilizzando i filtri nella funzione get_adjacent_post.

Nel file functions.php, aggiungere:

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

Poi nella tua pagina single.php aggiungere i filtri prima di chiamare i post precedenti/link seguente funzioni:

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

Se vuoi controllare per il tuo post_type specifico, puoi aggiungere un se intorno alla sezione aggiunta del filtro:

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

o, puoi semplicemente usare un file single.php specifico per post_type!

Questo ha funzionato grande per me, ma ci possono essere alcune limitazioni se si sta progettando sulla combinazione di questo con messaggi nella stesso termine ...

+1

La risposta di Danbrellis ha un apostrofo mancante. Non posso commentare in quanto questo è il mio primo account e non ho abbastanza rep per commentare. remove_filter ('get_previous_post_sort', mytheme_previous_post_orderby_name ', 10); Dovrebbe essere 'remove_filter ('get_previous_post_sort', 'mytheme_previous_post_orderby_name', 10);' (comunque anche con quella correzione, non riesco a farlo funzionare) – plumbinator

+0

plumbinator- grazie per averlo capito. ti stai avvicinando? Sono felice di dare un'occhiata se hai qualche codice di esempio. – danbrellis

Problemi correlati